PHP Memcached + APC + 文件緩存封裝實(shí)現(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);
}
}
?>
相關(guān)文章
PHP中信息格式化操作詳解(MessageFormatter類)
這篇文章主要給大家介紹了關(guān)于PHP中信息格式化操作的相關(guān)資料,主要運(yùn)用的是專門用于信息格式化的MessageFormatter類,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07編譯PHP報(bào)錯(cuò)configure error Cannot find libmysqlclient under usr的
這篇文章主要介紹了Linux上編譯PHP報(bào)錯(cuò)configure error Cannot find libmysqlclient under usr的解決方法,需要的朋友可以參考下2014-06-06解決file_get_contents無(wú)法請(qǐng)求https連接的方法
PHP.ini默認(rèn)配置下,用file_get_contents讀取https的鏈接,就會(huì)報(bào)如下錯(cuò)誤,本文給出解決方法2013-12-12PHP使用get_headers函數(shù)判斷遠(yuǎn)程文件是否存在的方法
這篇文章主要介紹了PHP使用get_headers函數(shù)判斷遠(yuǎn)程文件是否存在的方法,以實(shí)例形式分析了使用get_headers函數(shù)對(duì)遠(yuǎn)程文件是否存在進(jìn)行判斷的方法,以及針對(duì)重定向的排除方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11PHP實(shí)現(xiàn)通過(guò)CURL上傳文件功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)通過(guò)CURL上傳文件功能,結(jié)合實(shí)例形式分析了php使用curl文件上傳操作相關(guān)屬性設(shè)置與使用技巧,需要的朋友可以參考下2018-05-05PHP 正則表達(dá)式之正則處理函數(shù)小結(jié)(preg_match,preg_match_all,preg_replace,pr
本節(jié)我們就來(lái)介紹一下PHP中基于perl的正則表達(dá)式處理函數(shù),主要包含了分割, 匹配,查找,替換等等處理操作,依舊是配合示例講解,讓我們開(kāi)始吧2012-10-10php生成并下載word文件到本地實(shí)現(xiàn)方法詳解
要給最常用出租屋管理系統(tǒng)增加個(gè)合同功能,mark下知識(shí)點(diǎn)。要生成合同就需要使用phpword。文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08