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技术开发技巧分享
Mar 23 PHP
php中static静态变量的使用方法详解
Jun 04 PHP
php 代码优化之经典示例
Mar 24 PHP
PHP数组循环操作详细介绍 附实例代码
Feb 03 PHP
CMS中PHP判断系统是否已经安装的方法示例
Jul 26 PHP
php数组中删除元素之重新索引的方法
Sep 16 PHP
PHP MPDF中文乱码的解决方式
Dec 08 PHP
PHP+MYSQL实现读写分离简单实战
Mar 13 PHP
PHP设计模式之模板方法模式实例浅析
Dec 20 PHP
PHP生成指定范围内的N个不重复的随机数
Mar 18 PHP
YII2框架中日志的配置与使用方法实例分析
Mar 18 PHP
WordPress免插件实现面包屑导航的示例代码
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 daodb插入、更新与删除数据
2009/03/19 PHP
php将session放入memcached的设置方法
2014/02/14 PHP
php安装swoole扩展的方法
2015/03/19 PHP
浅谈php的优缺点
2015/07/14 PHP
Thinkphp 框架扩展之应用模式实现方法分析
2020/04/27 PHP
前后台交互过程中json格式如何解析以及如何生成
2012/12/26 Javascript
Jquery多选下拉列表插件jquery multiselect功能介绍及使用
2013/05/24 Javascript
js实现特定位取反原理及示例
2014/06/30 Javascript
javascript模块化简单解析
2016/04/07 Javascript
详解Node.Js如何处理post数据
2016/09/19 Javascript
前端js实现文件的断点续传 后端PHP文件接收
2016/10/14 Javascript
bootstrap导航栏、下拉菜单、表单的简单应用实例解析
2017/01/06 Javascript
原生JS实现图片翻书效果
2017/02/16 Javascript
JS实现双击内容变为可编辑状态
2017/03/03 Javascript
Vue中如何实现proxy代理
2018/04/20 Javascript
Vue中 key keep-alive的实现原理
2018/09/18 Javascript
微信小程序 获取手机号 JavaScript解密示例代码详解
2020/05/14 Javascript
Python内置的字符串处理函数详细整理(覆盖日常所用)
2014/08/19 Python
Python中关于使用模块的基础知识
2015/05/24 Python
python Django批量导入数据
2016/03/25 Python
浅谈Python中用datetime包进行对时间的一些操作
2016/06/23 Python
numpy创建单位矩阵和对角矩阵的实例
2019/11/29 Python
Python3操作读写CSV文件使用包过程解析
2020/04/10 Python
浅谈matplotlib中FigureCanvasXAgg的用法
2020/06/16 Python
Selenium alert 弹窗处理的示例代码
2020/08/06 Python
简单了解python关键字global nonlocal区别
2020/09/21 Python
CSS实现雨滴动画效果的实例代码
2019/10/08 HTML / CSS
解锁canvas导出图片跨域的N种姿势小结
2019/01/24 HTML / CSS
CAD制图设计师自荐信
2014/01/29 职场文书
平安建设实施方案
2014/03/19 职场文书
党的群众路线教育实践活动个人对照检查材料
2014/09/22 职场文书
出差报告格式模板
2014/11/06 职场文书
公司食堂管理制度
2015/08/05 职场文书
2016年父亲节寄语
2015/12/04 职场文书
因个人工作失误检讨书
2019/06/21 职场文书
关于golang高并发的实现与注意事项说明
2021/05/08 Golang