PHP Memcached + APC + 文件缓存封装实现代码


Posted in PHP onMarch 11, 2010

使用方法:
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 相关文章推荐
如何过滤高亮显示非法字符
Oct 09 PHP
一个连接两个不同MYSQL数据库的PHP程序
Oct 09 PHP
第五节--克隆
Nov 16 PHP
php购物车实现代码
Oct 10 PHP
PHP里8个鲜为人知的安全函数分析
Dec 09 PHP
php找出指定范围内回文数且平方根也是回文数的方法
Mar 23 PHP
php动态添加url查询参数的方法
Apr 14 PHP
php源码分析之DZX1.5随机数函数random用法
Jun 17 PHP
PHP处理CSV表格文件的常用操作方法总结
Jul 01 PHP
php文件上传 你真的掌握了吗
Nov 28 PHP
php获取当前月与上个月月初及月末时间戳的方法
Dec 05 PHP
PHP数组常用函数实例小结
Aug 20 PHP
了解Joomla 这款来自国外的php网站管理系统
Mar 11 #PHP
PHP调用Twitter的RSS的实现代码
Mar 10 #PHP
PHP中include()与require()的区别说明
Mar 10 #PHP
PHP扩展编写点滴 技巧收集
Mar 09 #PHP
php 修改zen-cart下单和付款流程以防止漏单
Mar 08 #PHP
PHP 最大运行时间 max_execution_time修改方法
Mar 08 #PHP
php ss7.5的数据调用 (笔记)
Mar 08 #PHP
You might like
PHP获取当前页面完整URL的实现代码
2013/06/10 PHP
PHP实现的购物车类实例
2015/06/17 PHP
使用JavaScript创建新样式表和新样式规则
2016/06/14 PHP
PHP执行shell脚本运行程序不产生core文件的方法
2016/12/28 PHP
php的优点总结 php有哪些优点
2019/07/19 PHP
js转义字符介绍
2013/11/05 Javascript
javascript中传统事件与现代事件
2015/06/23 Javascript
jQuery实现大转盘抽奖活动仿QQ音乐代码分享
2015/08/21 Javascript
微信小程序 wx.uploadFile无法上传解决办法
2016/12/14 Javascript
JavaScript函数表达式详解及实例
2017/05/05 Javascript
vue短信验证性能优化如何写入localstorage中
2018/04/25 Javascript
Vue路由history模式解决404问题的几种方法
2018/09/29 Javascript
Vue项目报错:Uncaught SyntaxError: Unexpected token
2018/11/10 Javascript
Element实现表格分页数据选择+全选所有完善批量操作
2019/06/07 Javascript
超简单的微信小程序轮播图
2019/11/22 Javascript
浅谈vue中document.getElementById()拿到的是原值的问题
2020/07/26 Javascript
[02:14]DOTA2英雄基础教程 修补匠
2013/12/23 DOTA
[10:54]Team Spirit vs Navi
2018/06/07 DOTA
关于python pyqt5安装失败问题的解决方法
2017/08/08 Python
python如何实现int函数的方法示例
2018/02/19 Python
CentOS7下python3.7.0安装教程
2018/07/30 Python
python3实现单目标粒子群算法
2019/11/14 Python
深入分析python 排序
2020/08/24 Python
用HTML5 实现橡皮擦的涂抹效果的教程
2015/05/11 HTML / CSS
Superdry极度乾燥官网:日本街头风格,纯英国制造品牌
2016/10/31 全球购物
LivingSocial爱尔兰:爱尔兰本地优惠
2018/08/10 全球购物
房屋租赁意向书
2014/04/01 职场文书
2014党员自我评议表范文
2014/09/20 职场文书
民事诉讼代理授权委托书
2014/10/11 职场文书
毕业生自荐信范文
2015/03/05 职场文书
电影建党伟业观后感
2015/06/01 职场文书
家长对学校的意见和建议
2015/06/03 职场文书
消防宣传标语大全
2015/08/03 职场文书
闭幕词的写作格式与范文!
2019/06/24 职场文书
2022漫威和DC电影上映作品
2022/04/05 欧美动漫
 分享一个Python 遇到数据库超好用的模块
2022/04/06 Python