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

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

主站蜘蛛池模板: 久久国产主播福利在线 | 好爽胸大好深好多水 | 亚洲三级在线中文字幕 | 暖暖在线观看播放视频 | 吃春药后的女教师 | 午夜A级理论片左线播放 | 广播电台在线收听 | 乱亲女H秽乱长久久久 | 亚洲精品中文字幕一二三四区 | 正在播放久久 | 日日噜噜夜夜狠狠扒开双腿 | 18禁在线无遮挡羞羞漫画 | 7723日本高清完整版在线观看 | 国产产一区二区三区久久毛片国语 | 嫩草成人国产精品 | 世界第一黄暴漫画家 | 最新无码国产在线视频2020 | 中文字幕一区中文亚洲 | 无码中文字幕热热久久 | 视频一区亚洲中文字幕 | 在线观看国产精美视频 | 高H高肉强J短篇NP | 日韩成人性视频 | 伊人成综合网伊人222 | 女仆翻身大作战 | 动漫H片在线播放免费高清 动漫AV纯肉无码AV电影网 | 亚洲国产无线码在线观看 | 亚洲最大在线视频 | 成人公开免费视频 | 美女扒开腿让男人桶个爽 | chinese东北夫妻video| 姐姐不~不可以动漫在线观看 | 特级毛片全部免费播放免下载 | 欧美高跟镣铐bdsm视频 | 免费看到湿的小黄文软件APP | 邻家美姨在线观看全集免费 | 真实国产乱子伦精品一区二区三区 | 8x8x我要打机飞在线观看 | 某上海少妇3P黑人完整版BD | 视频成人app永久在线观看 | 黑人巨大两根一起挤进欧美 |