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 相关文章推荐
简单的用PHP编写的导航条程序
Oct 09 PHP
PHP 开发环境配置(测试开发环境)
Apr 28 PHP
ThinkPHP与PHPExcel冲突解决方法
Aug 08 PHP
zf框架的校验器InArray使用示例
Mar 13 PHP
thinkphp特殊标签用法概述
Nov 24 PHP
php中print(),print_r(),echo()的区别详解
Dec 01 PHP
php防止sql注入简单分析
Mar 18 PHP
php开发工具有哪五款
Nov 09 PHP
PHP微信支付实例解析
Jul 22 PHP
PHP使用imagick扩展实现合并图像的方法
Apr 25 PHP
tp5框架前台无限极导航菜单类实现方法分析
Mar 29 PHP
php提高脚本性能的4个技巧
Aug 18 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保存带BOM文件的方法
2015/02/12 PHP
php数组冒泡排序算法实例
2016/05/06 PHP
php  PATH_SEPARATOR判断当前服务器系统类型实例
2016/10/28 PHP
php将服务端的文件读出来显示在web页面实例
2016/10/31 PHP
为何说PHP引用是个坑,要慎用
2018/04/02 PHP
玩转jQuery按钮 请告诉我你最喜欢哪些?
2012/01/08 Javascript
js如何获取file控件的完整路径具体实现代码
2013/05/15 Javascript
alert中断settimeout计时功能
2013/07/26 Javascript
JavaScript脚本判断蜘蛛来源的方法
2015/09/22 Javascript
Javascript闭包实例详解
2015/11/29 Javascript
JS实现图片局部放大或缩小的方法
2016/08/20 Javascript
微信小程序 for 循环详解
2016/10/09 Javascript
JS实现点击复选框变更DIV显示状态的示例代码
2017/12/18 Javascript
vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀
2018/06/13 Javascript
小程序图片剪裁加旋转的示例代码
2018/07/10 Javascript
详解webpack-dev-middleware 源码解读
2020/03/23 Javascript
JS原型对象操作实例分析
2020/06/06 Javascript
深入分析python数据挖掘 Json结构分析
2018/04/21 Python
pytorch 模拟关系拟合——回归实例
2020/01/14 Python
Python+OpenCV实现图像的全景拼接
2020/03/05 Python
Python PyQt5运行程序把输出信息展示到GUI图形界面上
2020/04/27 Python
Python matplotlib可视化实例解析
2020/06/01 Python
Python坐标轴操作及设置代码实例
2020/06/04 Python
用python对excel查重
2020/12/07 Python
python中翻译功能translate模块实现方法
2020/12/17 Python
使用css3实现超炫的loading加载动画效果
2014/05/07 HTML / CSS
LA MER海蓝之谜美国官网:传奇面霜
2016/08/27 全球购物
高中毕业自我鉴定
2013/12/19 职场文书
财务会计自荐信范文
2014/02/21 职场文书
《永远的白衣战士》教学反思
2014/04/25 职场文书
艾滋病宣传活动总结
2014/05/08 职场文书
五四演讲稿范文
2014/09/03 职场文书
税务干部群众路线教育实践活动对照检查材料
2014/09/20 职场文书
有限责任公司股东合作协议书范本
2014/10/30 职场文书
二十年同学聚会致辞
2015/07/28 职场文书
Python数据结构之队列详解
2022/03/21 Python