一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

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

主站蜘蛛池模板: 九九久久国产精品免费热6 九九久久精品 | 青青自拍视频一区二区三区 | 香蕉久久夜色精品国产2020 | 国产精品福利一区二区 | 亚洲成a人在线观看 | 97视频久久久 | 麻豆a| 国产精品一区二区久久 | 国产在线拍| 91视频中文字幕 | 久久亚洲私人国产精品 | 狠狠香蕉| 91全国探花精品正在播放 | 亚洲图片一区二区三区 | 大学生一级毛片全黄真人 | 久久噜国产精品拍拍拍拍 | 欧美激情中文字幕一区二区 | 亚洲春色另类小说 | 91精品国产免费 | 精品伊人久久久大香线蕉欧美 | 亚洲福利一区二区三区 | 成年美女黄| 色黄啪啪网18勿进 | 中文乱码精品一区二区三区 | 欧美成人精品久久精品 | 四虎精品视频在线永久免费观看 | 日韩激情文学 | 一区二区免费看 | 欧美一级做一a做片性视频 欧美一级做一级做片性十三 | 午夜视频在线 | 中文字幕日韩有码 | 国产精品免费精品自在线观看 | 午夜黄色福利视频 | 狠狠伊人 | 色老板视频在线观看 | 日本精品视频一区 | 视频一区二区三区在线 | 国产精品第页 | 99久草| 性欧美www | 在线精品免费视频 |