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-fpm超時(shí)時(shí)間設(shè)置request_terminate_timeout資源問(wèn)題分析
之前發(fā)現(xiàn)一個(gè)php配置之后關(guān)于返回500和502的問(wèn)題,今天看到一個(gè)兄弟寫(xiě)的非常不錯(cuò),記錄一下2019-09-09
PHP中查詢(xún)SQL Server或Sybase時(shí)TEXT字段被截?cái)嗟慕鉀Q方法
在CSDN的PHP版里老是看到有人問(wèn)TEXT字段被截?cái)嗟膯?wèn)題,偶也回答了無(wú)數(shù)次,今天索性就總結(jié)一下吧2009-03-03
php實(shí)現(xiàn)使用正則將文本中的網(wǎng)址轉(zhuǎn)換成鏈接標(biāo)簽
本文給大家分享一段php中使用正則表達(dá)式將網(wǎng)址轉(zhuǎn)換成A鏈接的函數(shù)代碼,十分簡(jiǎn)潔實(shí)用,這里推薦給大家2014-12-12
PHP實(shí)現(xiàn)微信掃碼登錄功能的兩種方式總結(jié)
這篇文章主要為大家介紹了利用PHP實(shí)現(xiàn)微信掃碼登錄功能的兩種方式,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定借鑒價(jià)值,需要的可以參考一下2022-08-08
php微信公眾平臺(tái)配置接口開(kāi)發(fā)程序
這篇文章主要為大家詳細(xì)介紹了php微信公眾平臺(tái)配置接口開(kāi)發(fā)程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
如何使用SublimeText3配置 PHP IDE環(huán)境
這篇文章主要介紹了如何使用SublimeText3配置 PHP IDE環(huán)境,并使用Xdebug進(jìn)行調(diào)試,喜歡使用SublimeText的同學(xué),可以參考下2021-04-04
phpmailer簡(jiǎn)單發(fā)送郵件的方法(附phpmailer源碼下載)
這篇文章主要介紹了phpmailer簡(jiǎn)單發(fā)送郵件的方法,提供了phpmailer的源碼與相應(yīng)的設(shè)置、使用方法,需要的朋友可以參考下2016-06-06
PHP 出現(xiàn)亂碼和Sessions驗(yàn)證問(wèn)題的解決方法!
PHP程序語(yǔ)言編碼一定要統(tǒng)一為UTF-8或GB2312如果選擇其他的語(yǔ)言在里面中文會(huì)出現(xiàn)亂碼的。2008-12-12

