天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP Memcached + APC + 文件緩存封裝實現(xiàn)代碼

使用方法:
Memcached
復制代碼 代碼如下:
$cache = new Cache_MemCache();
$cache->addServer('www1');
$cache->addServer('www2',11211,20); // this server has double the memory, and gets double the weight
$cache->addServer('www3',11211);
// Store some data in the cache for 10 minutes
$cache->store('my_key','foobar',600);
// Get it out of the cache again
echo($cache->fetch('my_key'));

文件緩存
復制代碼 代碼如下:
$cache = new Cache_File();
$key = 'getUsers:selectAll';
// check if the data is not in the cache already
if (!$data = $cache->fetch($key)) {
// assuming there is a database connection
$result = mysql_query("SELECT * FROM users");
$data = array();
// fetching all the data and putting it in an array
while($row = mysql_fetch_assoc($result)) { $data[] = $row; }
// Storing the data in the cache for 10 minutes
$cache->store($key,$data,600);
}

下載: class_cache3.php
復制代碼 代碼如下:
<?php

abstract class Cache_Abstract {
abstract function fetch($key);
abstract function store($key, $data, $ttl);
abstract function delete($key);
}

class Cache_APC extends Cache_Abstract {

function fetch($key) {
return apc_fetch($key);
}

function store($key, $data, $ttl) {
return apc_store($key, $data, $ttl);
}

function delete($key) {
return apc_delete($key);
}

}

class Cache_MemCache extends Cache_Abstract {
public $connection;

function __construct() {
$this->connection = new MemCache;
}

function store($key, $data, $ttl) {
return $this->connection->set($key, $data, 0, $ttl);
}

function fetch($key) {
return $this->connection->get($key);
}

function delete($key) {
return $this->connection->delete($key);
}

function addServer($host, $port = 11211, $weight = 10) {
$this->connection->addServer($host, $port, true, $weight);
}

}

class Cache_File extends Cache_Abstract {

function store($key, $data, $ttl) {
$h = fopen($this->getFileName($key), 'a+');
if (!$h)
throw new Exception('Could not write to cache');
flock($h, LOCK_EX);
fseek($h, 0);
ftruncate($h, 0);
$data = serialize(array(time() + $ttl, $data));
if (fwrite($h, $data) === false) {
throw new Exception('Could not write to cache');
}
fclose($h);
}

function fetch($key) {
$filename = $this->getFileName($key);
if (!file_exists($filename))
return false;
$h = fopen($filename, 'r');
if (!$h)
return false;
flock($h, LOCK_SH);
$data = file_get_contents($filename);
fclose($h);
$data = @ unserialize($data);
if (!$data) {
unlink($filename);
return false;
}
if (time() > $data[0]) {
unlink($filename);
return false;
}
return $data[1];
}

function delete($key) {
$filename = $this->getFileName($key);
if (file_exists($filename)) {
return unlink($filename);
}
else {
return false;
}
}

private function getFileName($key) {
return '/tmp/s_cache' . md5($key);
}

}
?>

php技術PHP Memcached + APC + 文件緩存封裝實現(xiàn)代碼,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 男女疯狂一边摸一边做羞羞视频 | 强被迫伦姧惨叫VIDEO | 91夫妻交友论坛 | 久草国产在线播放 | 欧美午夜福利主线路 | 嗯啊插的好深啊使劲抽视频 | 日韩少妇爆乳无码专区 | 日本黄色网站在线观看 | 国产成人精品区在线观看 | 青青青青青青草 | 东北女人一级毛片 | 亚洲色图另类小说 | 国产午夜精品久久久久九九 | 国产精品成人观看视频免费 | 国产成人h在线视频 | 天美麻豆成人AV精品视频 | 国产午夜人成在线视频麻豆 | 18禁无遮遮挡羞漫画免费阅读 | 午夜看片网| 亚洲欧美国产视频 | 日本高清免费一本视频在线观看 | 一级毛片两人添下面 | 性夜影院爽黄A爽免费动漫 性夜夜春夜夜爽AA片A | 无人区乱码区1卡2卡三卡在线 | 青青青久久 | 久久一级视频 | 欧美乱妇15p图 | 正在播放黑人杂交派对卧槽 | 青青青青青青青草 | 亚洲欧洲日产国码久在线 | 91久久综合精品国产丝袜长腿 | 91se在线看片国产免费观看 | 无遮18禁在线永久免费观看挡 | 久久电影精品久久99久久 | 诱人的女邻居9中文观看 | 最近的2019中文字幕国语 | 年轻的老师5理伦片 | 轻点慢点1V2啊高H抽插 | xxx在线播放 | 日韩中文亚洲欧美视频二 | 全球真实小U女视频合集 |