天天躁日日躁狠狠躁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 + 文件緩存封裝實現代碼,轉載需保留來源!

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

主站蜘蛛池模板: 天天躁日日躁狠狠躁午夜剧场 | 97午夜伦伦电影理论片 | 一区三区三区不卡 | 97精品国产自产在线观看永久 | 日本高清免费看 | 精品视频久久久久 | 国产精品亚洲精品日韩电影 | 午夜视频在线网站 | 一二三四韩国免费观看 | 久久精品国产在热亚洲完整版 | 邻家美姨在线观看全集免费 | 国内精品久久久久久西瓜色吧 | 99久久国产露脸精品国产麻豆 | 免费在线观看一区 | YELLOW视频直播在线观看 | 娇小8一12xxxx第一次 | 中文字幕爆乳JULIA女教师 | 国内精品不卡一区二区三区 | 超碰在线 视频 | 97人妻无码AV碰碰视频 | 公和我做好爽添厨房中文字幕 | 诱人的女邻居9中文观看 | 色妞色视频一区二区三区四区 | 男生脱美女内裤内衣动态图 | 伊人香蕉在线播放视频免费 | 啊灬啊别停灬用力啊老师 | 恋夜影院支持安卓视频美女 | 精精国产www视频在线观看免费 | av淘宝 在线观看 | 挺进老师的紧窄小肉六电影完整版 | 亚洲欧美一区二区成人片 | 少妇高潮惨叫久久久久久电影 | 色女仆影院 | 暖暖 免费 高清 日本视频5 | 99久久爱看免费观看 | 四库影院永久国产精品 | 久久久久青草大香线综合精品 | 爆操大胸美女 | 呻吟翘臀后进爆白浆 | 国产亚洲精品福利视频 | 青青青青青青草 |