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
php你的验证码安全码?
Jan 02 PHP
关于mysql 字段的那个点为是定界符
Jan 15 PHP
php 无限极分类
Mar 27 PHP
str_replace只替换一次字符串的方法
Apr 09 PHP
PHP 正则表达式常用函数
Aug 17 PHP
PHP实现的比较完善的购物车类
Dec 02 PHP
php编译安装php-amq扩展简明教程
Jun 25 PHP
yii的入口文件index.php中为什么会有这两句
Aug 04 PHP
PHP控制反转(IOC)和依赖注入(DI)
Mar 13 PHP
ThinkPHP类似AOP思想的参数验证的实现方法
Dec 18 PHP
浅谈如何提高PHP代码质量之端到端集成测试
May 28 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三元运算符的结合性介绍
2012/01/10 PHP
PHP实现的简易版图片相似度比较
2015/01/07 PHP
详解PHP的抽象类和抽象方法以及接口总结
2019/03/15 PHP
js 为label标签和div标签赋值的方法
2013/08/08 Javascript
代码触发js事件(click、change)示例应用
2013/12/13 Javascript
一个不错的字符串转码解码函数(自写)
2014/07/31 Javascript
js实现文本框只允许输入数字并限制数字大小的方法
2015/08/19 Javascript
jquery $.trim()去除字符串空格的实现方法【附图例】
2016/03/30 Javascript
AngularJS基础 ng-cut 指令介绍及简单示例
2016/08/01 Javascript
Three.js学习之网格
2016/08/10 Javascript
使用JavaScript获取URL中的参数(两种方法)
2016/11/16 Javascript
Web 开发中Ajax的Session 超时处理方法
2017/01/19 Javascript
利用jquery去掉时光轴头尾部线条的方法实例
2017/06/16 jQuery
微信小程序自动客服功能
2017/11/02 Javascript
Vue2.0用户权限控制解决方案
2017/11/29 Javascript
《javascript设计模式》学习笔记七:Javascript面向对象程序设计组合模式详解
2020/04/08 Javascript
详解Vue2的diff算法
2021/01/06 Vue.js
python 队列详解及实例代码
2016/10/18 Python
django模板加载静态文件的方法步骤
2019/03/01 Python
使用python分析统计自己微信朋友的信息
2019/07/19 Python
Python数据可视化实现正态分布(高斯分布)
2019/08/21 Python
np.newaxis 实现为 numpy.ndarray(多维数组)增加一个轴
2019/11/30 Python
使用Tensorflow实现可视化中间层和卷积层
2020/01/24 Python
Python 基于FIR实现Hilbert滤波器求信号包络详解
2020/02/26 Python
如何用Python 加密文件
2020/09/10 Python
Python:__eq__和__str__函数的使用示例
2020/09/26 Python
一个基于canvas的移动端图片编辑器的实现
2020/10/28 HTML / CSS
北美领先的牛仔品牌:Buffalo David Bitton
2017/05/22 全球购物
struct和class的区别
2015/11/20 面试题
白酒市场营销方案
2014/02/25 职场文书
《狮子和兔子》教学反思
2014/03/02 职场文书
介绍信格式
2015/01/30 职场文书
违纪开除通知书
2015/04/25 职场文书
奖学金申请书(范文)
2019/08/14 职场文书
详解Html5项目适配系统深色模式方案总结
2021/04/14 HTML / CSS
小米11和iphone12哪个值得买?小米11对比iphone12评测
2021/04/21 数码科技