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 相关文章推荐
E路文章系统PHP
Dec 11 PHP
PHP has encountered an Access Violation at 7C94BD02解决方法
Aug 24 PHP
php全排列递归算法代码
Oct 09 PHP
解析VS2010利用VS.PHP插件调试PHP的方法
Jul 19 PHP
PHP实现更新中间关联表数据的两种方法
Sep 01 PHP
PHP session文件独占锁引起阻塞问题解决方法
May 12 PHP
举例详解PHP脚本的测试方法
Aug 05 PHP
Symfony2中被遗弃的getRequest()方法分析
Mar 17 PHP
又拍云异步上传实例教程详解
Apr 19 PHP
PHP获取指定日期是星期几的实现方法
Nov 30 PHP
Laravel框架路由和控制器的绑定操作方法
Jun 12 PHP
laravel5.6实现数值转换
Oct 23 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模板的朋友必看的很多个顶级PHP模板引擎比较分析
2008/05/26 PHP
PHP中$_SERVER使用说明
2015/07/05 PHP
解决tp5在nginx下修改配置访问的问题
2019/10/16 PHP
window.location.reload()方法刷新页面弹出要再次显示该网页对话框
2013/04/24 Javascript
解析URI与URL之间的区别与联系
2013/11/22 Javascript
当jQuery1.7遇上focus方法的问题
2014/01/26 Javascript
jquery实现网页查找功能示例分享
2014/02/12 Javascript
JavaScript错误处理
2015/02/03 Javascript
javascript实现标签切换代码示例
2016/05/22 Javascript
yarn的使用与升级Node.js的方法详解
2017/06/04 Javascript
vue学习笔记之v-if和v-show的区别
2017/09/20 Javascript
Bootstrap实现的表格合并单元格示例
2018/02/06 Javascript
vue-cli3 DllPlugin 提取公用库的方法
2019/04/24 Javascript
Vue父组件向子组件传值以及data和props的区别详解
2020/03/02 Javascript
Vue如何实现监听组件原生事件
2020/07/03 Javascript
聊聊vue 中的v-on参数问题
2021/01/29 Vue.js
python基础教程之自定义函数介绍
2014/08/29 Python
Python threading多线程编程实例
2014/09/18 Python
django 在原有表格添加或删除字段的实例
2018/05/27 Python
基于数据归一化以及Python实现方式
2018/07/11 Python
Python后台管理员管理前台会员信息的讲解
2019/01/28 Python
pytorch使用 to 进行类型转换方式
2020/01/08 Python
tensorflow获取预训练模型某层参数并赋值到当前网络指定层方式
2020/01/24 Python
PyCharm+Pipenv虚拟环境开发和依赖管理的教程详解
2020/04/16 Python
selenium+headless chrome爬虫的实现示例
2021/01/08 Python
服装厂厂长岗位职责
2013/12/27 职场文书
小学教师听课制度
2014/02/01 职场文书
酒店员工培训方案
2014/06/02 职场文书
普通党员对照检查材料
2014/09/24 职场文书
公司感恩节活动策划书
2014/10/11 职场文书
服务承诺书
2015/01/19 职场文书
颐和园的导游词
2015/01/30 职场文书
学生退学证明
2015/06/23 职场文书
小学校长开学致辞
2015/07/29 职场文书
任命书格式范文
2015/09/22 职场文书
公司转让协议书
2016/03/19 职场文书