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

PHP Memcached + APC + 文件緩存封裝實現代碼

使用方法:
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 + 文件緩存封裝實現代碼,轉載需保留來源!

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

主站蜘蛛池模板: 99国产电影 | 高h肉文np | 国产精品免费久久久久影院 | 亚洲精品乱码电影在线观看 | 亚洲一卡久久4卡5卡6卡7卡 | 国产精品美女久久久久AV超清 | 5580免费午夜福利院 | 91麻豆精品国产一级 | 日产亚洲一区二区三区 | 水蜜桃亚洲一二三四在线 | 99久久精品免费看国产一区二区三区 | GOGOGO高清免费播放 | 伊人影院香蕉久在线26 | 亚洲欧美偷拍视频一区 | 亚洲中文字幕乱码熟女在线 | 久久精品中文闷骚内射 | 久久久精品久久久久三级 | 体育生爆操 | 国产精品成人久久久久A伋 国产精品成人观看视频免费 | 国产精品永久免费 | 99国产精品久久久久久久日本竹 | AV无码国产精品午夜A片麻豆 | 国产AV一区二区三区日韩 | 泷泽萝拉首部av | 国产传媒18精品A片在线观看 | 色偷偷超碰97人人澡人人 | 国产成人刺激视频在线观看 | 亚洲人成在线观看一区二区 | 国产午夜亚洲精品区 | 欧美日韩国产码在线 | YELLOW视频在线观看最新 | 国产一卡2卡3卡4卡孕妇网站 | 麻豆免费高清完整版 | 久久视频这里只精品99热在线 | bt天堂午夜国产精品 | 嗨嗨快播电影 | 全是肉的高h短篇列车 | 久久国产精品人妻中文 | 狠狠色综合7777久夜色撩人 | 91九色麻豆 | 袖珍人与大黑人性视频 |