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 相关文章推荐
phpMyadmin 用户权限中英对照
Apr 02 PHP
PHP开发中常用的字符串操作函数
Feb 08 PHP
php中OR与|| AND与&amp;&amp;的区别总结
Oct 26 PHP
php常用字符串比较函数实例汇总
Nov 24 PHP
php用ini_get获取php.ini里变量值的方法
Mar 04 PHP
PHP多进程编程总结(推荐)
Jul 18 PHP
Yii2中多表关联查询hasOne hasMany的方法
Feb 15 PHP
PHP使用星号替代用户名手机和邮箱的实现代码
Feb 07 PHP
PHP实现动态添加XML中数据的方法
Mar 30 PHP
PHP生成(支持多模板)二维码海报代码
Apr 30 PHP
PHP实现数组和对象的相互转换操作示例
Mar 20 PHP
thinkphp3.2同时连接两个数据库的简单方法
Aug 13 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
一个显示天气预报的程序
2006/10/09 PHP
PHP生成不重复随机数的方法汇总
2014/11/19 PHP
js 全兼容可高亮二级缓冲折叠菜单
2010/06/04 Javascript
基于jquery的lazy loader插件实现图片的延迟加载[简单使用]
2011/05/07 Javascript
改变文件域的样式实现思路同时兼容ie、firefox
2013/10/23 Javascript
jQuery设置div一直在页面顶部显示的方法
2013/10/24 Javascript
JavaScript和CSS交互的方法汇总
2014/12/02 Javascript
jQuery实现表格行上下移动和置顶效果
2015/06/05 Javascript
javascript父子页面通讯实例详解
2015/07/17 Javascript
JS面向对象(3)之Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
2016/02/25 Javascript
微信小程序 在Chrome浏览器上运行以及WebStorm的使用
2016/09/27 Javascript
JS基于面向对象实现的选项卡效果示例
2016/12/20 Javascript
微信小程序项目实践之九宫格实现及item跳转功能
2018/07/19 Javascript
详解如何用VUE写一个多用模态框组件模版
2018/09/27 Javascript
jQuery动态生成的元素绑定事件操作实例分析
2019/05/04 jQuery
vue v-for 使用问题整理小结
2019/08/04 Javascript
Vue实现购物车详情页面的方法
2019/08/20 Javascript
微信小程序监听用户登录事件的实现方法
2019/11/11 Javascript
javascript用defineProperty实现简单的双向绑定方法
2020/04/03 Javascript
python通过pil将图片转换成黑白效果的方法
2015/03/16 Python
Python编程django实现同一个ip十分钟内只能注册一次
2017/11/03 Python
Python实现使用卷积提取图片轮廓功能示例
2018/05/12 Python
PYTHON EVAL的用法及注意事项解析
2019/09/06 Python
一篇文章搞懂python的转义字符及用法
2020/09/03 Python
python excel多行合并的方法
2020/12/09 Python
HTML5 canvas基本绘图之绘制曲线
2016/06/27 HTML / CSS
草莓巧克力:Shari’s Berries
2017/02/07 全球购物
大学英语演讲稿(中英文对照)
2014/01/14 职场文书
多媒体编辑专业毕业生求职信
2014/06/13 职场文书
基层干部群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
一年级数学下册复习计划
2015/01/17 职场文书
解决golang post文件时Content-Type出现的问题
2021/05/02 Golang
斗罗大陆八大特殊魂兽,龙族始祖排榜首,第五最残忍(翠魔鸟)
2022/03/18 国漫
mapstruct的用法之qualifiedByName示例详解
2022/04/06 Java/Android
Python OpenCV形态学运算示例详解
2022/04/07 Python
Python 操作pdf pdfplumber读取PDF写入Exce
2022/08/14 Python