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 数据同步
Oct 09 PHP
PHP 已经成熟
Dec 04 PHP
PHP分页显示制作详细讲解
Nov 19 PHP
PHP乱码问题,UTF-8乱码常见问题小结
Apr 09 PHP
兼容PHP和Java的des加密解密代码分享
Jun 26 PHP
ThinkPHP分页实例
Oct 15 PHP
php生成随机颜色方法汇总
Dec 03 PHP
PHP程序中的文件锁、互斥锁、读写锁使用技巧解析
Mar 21 PHP
今天你说520了吗?不仅有php表白书还有java表白神器
May 20 PHP
PHP实现上传图片到 zimg 服务器
Oct 19 PHP
使用PHPExcel实现数据批量导出为excel表格的方法(必看)
Jun 09 PHP
tp5修改(实现即点即改)
Oct 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中全局变量global和$GLOBALS[]的区别分析
2012/08/06 PHP
PHP图片处理之图片旋转和图片翻转实例
2014/11/19 PHP
php mysql_list_dbs()函数用法示例
2017/03/29 PHP
thinkphp5.0自定义验证规则使用方法
2017/11/16 PHP
laravel 框架结合关联查询 when()用法分析
2019/11/22 PHP
js身份证验证超强脚本
2008/10/26 Javascript
3分钟写出来的Jquery版checkbox全选反选功能
2013/10/23 Javascript
js 获取元素下面所有li的两种方法
2014/04/14 Javascript
javascript操作字符串的原生方法
2014/12/22 Javascript
检测一个函数是否是JavaScript原生函数的小技巧
2015/03/13 Javascript
JS中判断null的方法分析
2016/11/21 Javascript
jQuery+HTML5实现WebGL高性能烟花绽放动画效果【附demo源码下载】
2017/08/18 jQuery
深入理解ES6 Promise 扩展always方法
2017/09/26 Javascript
浅谈vue首屏加载优化
2018/06/28 Javascript
在vue中获取微信支付code及code被占用问题的解决方法
2019/04/16 Javascript
[05:08]2014DOTA2国际邀请赛 Hao专访复仇的胜利很爽
2014/07/15 DOTA
在Python程序中操作MySQL的基本方法
2015/07/29 Python
Python探索之Metaclass初步了解
2017/10/28 Python
Python基于PyGraphics包实现图片截取功能的方法
2017/12/21 Python
Python利用字典将两个通讯录文本合并为一个文本实例
2018/01/16 Python
基于wxPython的GUI实现输入对话框(2)
2019/02/27 Python
python网络应用开发知识点浅析
2019/05/28 Python
python实现生成Word、docx文件的方法分析
2019/08/30 Python
python爬虫-模拟微博登录功能
2019/09/12 Python
pytorch 实现cross entropy损失函数计算方式
2020/01/02 Python
Wilson体育用品官网:美国著名运动器材品牌
2019/05/12 全球购物
Prototype如何实现页面局部定时刷新
2013/08/06 面试题
社区优秀志愿者材料
2014/02/02 职场文书
求职意向书
2014/04/01 职场文书
生物工程专业求职信
2014/09/03 职场文书
计划生育证明书写要求
2014/09/17 职场文书
刑事和解协议书范本
2014/11/19 职场文书
分享15个Webpack实用的插件!!!
2021/03/31 Javascript
html5移动端禁止长按图片保存的实现
2021/04/20 HTML / CSS
MySQL的Query Cache图文详解
2021/07/01 MySQL
MySQL8.0升级的踩坑历险记
2021/11/01 MySQL