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 批量删除 sql语句
Jun 05 PHP
PHP 存储文本换行实现方法
Jan 05 PHP
php入门学习知识点三 PHP上传
Jul 14 PHP
解析php取整的几种方式
Jun 25 PHP
php仿QQ验证码的实例分析
Jul 01 PHP
几道坑人的PHP面试题 试试看看你会不会也中招
Aug 19 PHP
php与Mysql的一些简单的操作
Feb 26 PHP
Windows下Apache + PHP SESSION丢失的解决过程全纪录
Apr 07 PHP
smarty学习笔记之常见代码段用法总结
Mar 19 PHP
PHP+iframe图片上传实现即时刷新效果
Nov 18 PHP
Laravel框架实现的批量删除功能示例
Jan 16 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/10/04 PHP
php object转数组示例
2014/01/15 PHP
Yii数据读取与跳转参数传递用法实例分析
2016/07/12 PHP
PHP实现的下载远程文件类定义与用法示例
2017/07/05 PHP
php菜单/评论数据递归分级算法的实现方法
2019/08/01 PHP
Yii 框架使用Forms操作详解
2020/05/18 PHP
Html中JS脚本执行顺序简单举例说明
2010/06/19 Javascript
javascript实现des解密加密全过程
2014/04/03 Javascript
JS实现静止元素自动移动示例
2014/04/14 Javascript
在线所见即所得HTML编辑器的实现原理浅析
2015/04/25 Javascript
JavaScript中用sort()方法对数组元素进行排序的操作
2015/06/09 Javascript
js 获取站点应用名的简单实例
2016/08/18 Javascript
js实现短信发送倒计时功能(正则验证)
2017/02/10 Javascript
浅谈键盘上回车按钮的js触发事件
2017/02/13 Javascript
关于Bootstrap按钮组件消除黄框的方法
2017/05/19 Javascript
webpack+vue-cli项目中引入外部非模块格式js的方法
2018/09/28 Javascript
JavaScript函数的4种调用方法实例分析
2019/03/05 Javascript
JS实现判断数组是否包含某个元素示例
2019/05/24 Javascript
webpack安装配置与常见使用过程详解(结合vue)
2020/06/01 Javascript
python通过cookie模拟已登录状态的初步研究
2016/11/09 Python
python实现树形打印目录结构
2018/03/29 Python
对python:print打印时加u的含义详解
2018/12/15 Python
基于Python实现视频的人脸融合功能
2020/06/12 Python
python中strip(),lstrip(),rstrip()函数的使用讲解
2020/11/17 Python
法国在线药房:DoctiPharma
2020/10/21 全球购物
英国排名第一的冲浪店:Ann’s Cottage
2020/06/21 全球购物
当文件系统受到破坏时,如何检查和修复系统?
2012/03/09 面试题
实习医生自我评价
2013/09/22 职场文书
《十六年前的回忆》教学反思
2014/02/14 职场文书
基层工作经验证明样本
2014/11/16 职场文书
初婚初育证明范本
2014/11/24 职场文书
政审证明材料
2015/06/19 职场文书
求职信:会计求职的写作技巧
2019/04/24 职场文书
2019最新激励员工口号大全!
2019/06/28 职场文书
创业计划书之养殖业
2019/10/11 职场文书
用Python仅20行代码编写一个简单的端口扫描器
2022/04/08 Python