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 相关文章推荐
使用 MySQL 开始 PHP 会话
Dec 21 PHP
ThinkPHP php 框架学习笔记
Oct 30 PHP
Apache环境下PHP利用HTTP缓存协议原理解析及应用分析
Feb 16 PHP
php 连接mssql数据库 初学php笔记
Mar 01 PHP
PHP foreach循环使用详解与实例代码
May 08 PHP
php fsockopen中多线程问题的解决办法[翻译]
Nov 09 PHP
php 不使用js实现页面跳转
Feb 11 PHP
PHP读取大文件的类SplFileObject使用介绍
Apr 09 PHP
PHP网站建设的流程与步骤分享
Sep 25 PHP
PHP实现的简单在线计算器功能示例
Aug 02 PHP
php使用curl下载指定大小的文件实例代码
Sep 30 PHP
PHP设计模式之建造者模式(Builder)原理与用法案例详解
Dec 12 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代码保护--Zend Guard的使用详解
2013/06/03 PHP
php内嵌函数用法实例
2015/03/20 PHP
Laravel 关联模型-关联新增和关联更新的方法
2019/10/10 PHP
Jquery replace 字符替换实现代码
2010/12/02 Javascript
javascript suggest效果 自动完成实现代码分享
2012/02/17 Javascript
JavaScript高级程序设计 读书笔记之九 本地对象Array
2012/02/27 Javascript
如何使用Javascript正则表达式来格式化XML内容
2013/07/04 Javascript
jQuery中验证表单提交方式及序列化表单内容的实现
2014/01/06 Javascript
JS中的数组的sort方法使用示例
2014/01/22 Javascript
js实现的GridView即表头固定表体有滚动条且可滚动
2014/02/19 Javascript
js模拟C#中List的简单实例
2014/03/06 Javascript
jQuery中:selected选择器用法实例
2015/01/04 Javascript
jQuery实现给页面换肤的方法
2015/05/30 Javascript
jquery中添加属性和删除属性
2015/06/03 Javascript
解决JS请求服务器gbk文件乱码的问题
2015/10/16 Javascript
浅谈javascript中new操作符的原理
2016/06/07 Javascript
使用jquery给指定的table动态添加一行、删除一行
2016/10/13 Javascript
Vue学习笔记进阶篇之多元素及多组件过渡
2017/07/19 Javascript
Web技术实现移动监测的介绍
2017/09/18 Javascript
浅谈Angular路由复用策略
2017/10/04 Javascript
es6函数之严格模式用法实例分析
2020/03/17 Javascript
jQuery 选择器用法实例分析【prev + next】
2020/05/22 jQuery
解决vue安装less报错Failed to compile with 1 errors的问题
2020/10/22 Javascript
[00:32]2018DOTA2亚洲邀请赛iG出场
2018/04/03 DOTA
[42:24]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第三场 11.27
2020/12/01 DOTA
[01:23:45]DOTA2-DPC中国联赛 正赛 CDEC vs Dragon BO3 第一场 1月22日
2021/03/11 DOTA
Python中使用item()方法遍历字典的例子
2014/08/26 Python
200行python代码实现2048游戏
2019/07/17 Python
Python 图像对比度增强的几种方法(小结)
2019/09/25 Python
巴西婴儿用品商店:Bebe Store
2017/11/23 全球购物
西班牙在线光学:Visual-Click
2020/06/22 全球购物
小学校园之星事迹材料
2014/05/16 职场文书
英语课前三分钟演讲稿(6篇)
2014/09/13 职场文书
小学班主任事迹材料
2014/12/17 职场文书
解决Maven项目中 Invalid bound statement 无效的绑定问题
2021/06/15 Java/Android
Oracle中update和select 关联操作
2022/01/18 Oracle