来自phpguru得Php Cache类源码


Posted in PHP onApril 15, 2010

Cache的作用不用说大家都知道咯,这些天也面试了一些人,发现很多人框架用多了,基础都忘记了,你问一些事情,他总是说框架解决了,而根本不明白是怎么回事,所以也提醒大家应该注意平时基础知识的积累,之后对一些问题才能游刃有余.

群里也有些朋友对基础知识很不屑,总说有能力就可以了,基础知识考不出来什么.对于这样的观点,我一直不苟同.
这个只是一点感概罢了. 下面看正题,介绍一个php的Cache类:

贴一下代码吧:下面也有下载地址,其实很简单,重要的是学习

<?php 
/** 
* o------------------------------------------------------------------------------o 
* | This package is licensed under the Phpguru license. A quick summary is | 
* | that for commercial use, there is a small one-time licensing fee to pay. For | 
* | registered charities and educational institutes there is a reduced license | 
* | fee available. You can read more at: | 
* | | 
* | http://www.phpguru.org/static/license.html | 
* o------------------------------------------------------------------------------o 
*/ 
/** 
* Caching Libraries for PHP5 
* 
* Handles data and output caching. Defaults to /dev/shm 
* (shared memory). All methods are static. 
* 
* Eg: (output caching) 
* 
* if (!OutputCache::Start('group', 'unique id', 600)) { 
* 
* // ... Output 
* 
* OutputCache::End(); 
* } 
* 
* Eg: (data caching) 
* 
* if (!$data = DataCache::Get('group', 'unique id')) { 
* 
* $data = time(); 
* 
* DataCache::Put('group', 'unique id', 10, $data); 
* } 
* 
* echo $data; 
*/ 
class Cache 
{ 
/** 
* Whether caching is enabled 
* @var bool 
*/ 
public static $enabled = true; 
/** 
* Place to store the cache files 
* @var string 
*/ 
protected static $store = '/dev/shm/'; 
/** 
* Prefix to use on cache files 
* @var string 
*/ 
protected static $prefix = 'cache_'; 
/** 
* Stores data 
* 
* @param string $group Group to store data under 
* @param string $id Unique ID of this data 
* @param int $ttl How long to cache for (in seconds) 
*/ 
protected static function write($group, $id, $ttl, $data) 
{ 
$filename = self::getFilename($group, $id); 
if ($fp = @fopen($filename, 'xb')) { 
if (flock($fp, LOCK_EX)) { 
fwrite($fp, $data); 
} 
fclose($fp); 
// Set filemtime 
touch($filename, time() + $ttl); 
} 
} 
/** 
* Reads data 
* 
* @param string $group Group to store data under 
* @param string $id Unique ID of this data 
*/ 
protected static function read($group, $id) 
{ 
$filename = self::getFilename($group, $id); 
return file_get_contents($filename); 
} 
/** 
* Determines if an entry is cached 
* 
* @param string $group Group to store data under 
* @param string $id Unique ID of this data 
*/ 
protected static function isCached($group, $id) 
{ 
$filename = self::getFilename($group, $id); 
if (self::$enabled && file_exists($filename) && filemtime($filename) > time()) { 
return true; 
} 
@unlink($filename); 
return false; 
} 
/** 
* Builds a filename/path from group, id and 
* store. 
* 
* @param string $group Group to store data under 
* @param string $id Unique ID of this data 
*/ 
protected static function getFilename($group, $id) 
{ 
$id = md5($id); 
return self::$store . self::$prefix . "{$group}_{$id}"; 
} 
/** 
* Sets the filename prefix to use 
* 
* @param string $prefix Filename Prefix to use 
*/ 
public static function setPrefix($prefix) 
{ 
self::$prefix = $prefix; 
} 
/** 
* Sets the store for cache files. Defaults to 
* /dev/shm. Must have trailing slash. 
* 
* @param string $store The dir to store the cache data in 
*/ 
public static function setStore($store) 
{ 
self::$store = $store; 
} 
} 
/** 
* Output Cache extension of base caching class 
*/ 
class OutputCache extends Cache 
{ 
/** 
* Group of currently being recorded data 
* @var string 
*/ 
private static $group; 
/** 
* ID of currently being recorded data 
* @var string 
*/ 
private static $id; 
/** 
* Ttl of currently being recorded data 
* @var int 
*/ 
private static $ttl; 
/** 
* Starts caching off. Returns true if cached, and dumps 
* the output. False if not cached and start output buffering. 
* 
* @param string $group Group to store data under 
* @param string $id Unique ID of this data 
* @param int $ttl How long to cache for (in seconds) 
* @return bool True if cached, false if not 
*/ 
public static function Start($group, $id, $ttl) 
{ 
if (self::isCached($group, $id)) { 
echo self::read($group, $id); 
return true; 
} else { 
ob_start(); 
self::$group = $group; 
self::$id = $id; 
self::$ttl = $ttl; 
return false; 
} 
} 
/** 
* Ends caching. Writes data to disk. 
*/ 
public static function End() 
{ 
$data = ob_get_contents(); 
ob_end_flush(); 
self::write(self::$group, self::$id, self::$ttl, $data); 
} 
} 
/** 
* Data cache extension of base caching class 
*/ 
class DataCache extends Cache 
{ 
/** 
* Retrieves data from the cache 
* 
* @param string $group Group this data belongs to 
* @param string $id Unique ID of the data 
* @return mixed Either the resulting data, or null 
*/ 
public static function Get($group, $id) 
{ 
if (self::isCached($group, $id)) { 
return unserialize(self::read($group, $id)); 
} 
return null; 
} 
/** 
* Stores data in the cache 
* 
* @param string $group Group this data belongs to 
* @param string $id Unique ID of the data 
* @param int $ttl How long to cache for (in seconds) 
* @param mixed $data The data to store 
*/ 
public static function Put($group, $id, $ttl, $data) 
{ 
self::write($group, $id, $ttl, serialize($data)); 
} 
} 
?>

使用方法:
$dir = !empty($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '.'; 
$dh = opendir($dir); 
while ($filename = readdir($dh)) { 
if ($filename == '.' OR $filename == '..') { 
continue; 
} 
if (filemtime($dir . DIRECTORY_SEPARATOR . $filename) < time()) { 
unlink($dir . DIRECTORY_SEPARATOR . $filename); 
} 
}

源码打包下载
PHP 相关文章推荐
php 购物车的例子
May 04 PHP
谈谈新手如何学习PHP 默默经典版本
Aug 04 PHP
深入PHP数据缓存的使用说明
May 10 PHP
zf框架的db类select查询器join链表使用示例(zend框架)
Mar 14 PHP
PHP学习笔记(二):变量详解
Apr 17 PHP
php无序树实现方法
Jul 28 PHP
ThinkPHP模板循环输出Volist标签用法实例详解
Mar 23 PHP
php有效防止图片盗用、盗链的两种方法
Nov 01 PHP
php 猴子摘桃的算法
Jun 20 PHP
Laravel接收前端ajax传来的数据的实例代码
Jul 20 PHP
laravel框架模型、视图与控制器简单操作示例
Oct 10 PHP
Laravel框架实现抢红包功能示例
Oct 31 PHP
php cache类代码(php数据缓存类)
Apr 15 #PHP
PHP中防止SQL注入攻击和XSS攻击的两个简单方法
Apr 15 #PHP
php 格式化数字的时候注意数字的范围
Apr 13 #PHP
在IIS7.0下面配置PHP 5.3.2运行环境的方法
Apr 13 #PHP
php 上传功能实例代码
Apr 13 #PHP
php array_search() 函数使用
Apr 13 #PHP
php in_array 函数使用说明与in_array需要注意的地方说明
Apr 13 #PHP
You might like
增加反向链接的101个方法 站长推荐
2007/01/31 PHP
如何使用php判断服务器是否是HTTPS连接
2013/07/05 PHP
PHP的RSA加密解密方法以及开发接口使用
2018/02/11 PHP
js 回车提交表单两种实现方法
2012/12/31 Javascript
javascript中的undefined和not defined区别示例介绍
2014/02/26 Javascript
JavaScript把数组作为堆栈使用的方法
2015/03/20 Javascript
jQuery实现定时读取分析xml文件的方法
2015/07/16 Javascript
jquery衣服颜色选取插件效果代码分享
2015/08/28 Javascript
JS简单实现多级Select联动菜单效果代码
2015/09/06 Javascript
Javascript 对cookie操作详解及实例
2016/12/29 Javascript
vue项目中使用axios上传图片等文件操作
2017/11/02 Javascript
JS简单实现查看文档创建日期、修改日期和文档大小的方法示例
2018/04/08 Javascript
vue-cli 3.x 配置Axios(proxyTable)跨域代理方法
2018/09/19 Javascript
vue 项目地址去掉 #的方法
2018/10/20 Javascript
微信小程序购物车、父子组件传值及calc的注意事项总结
2018/11/14 Javascript
新手快速入门JavaScript装饰者模式与AOP
2019/06/24 Javascript
JQuery插件tablesorter表格排序实现过程解析
2020/05/28 jQuery
vue路由权限校验功能的实现代码
2020/06/07 Javascript
vue实现列表拖拽排序的功能
2020/11/02 Javascript
JavaScript实现浏览器网页自动滚动并点击的示例代码
2020/12/05 Javascript
Python单链表的简单实现方法
2014/09/23 Python
对python中的xlsxwriter库简单分析
2018/05/04 Python
Python中list查询及所需时间计算操作示例
2018/06/21 Python
pyinstaller打包单文件时--uac-admin选项不起作用怎么办
2020/04/15 Python
什么是python的自省
2020/06/21 Python
如何解决安装python3.6.1失败
2020/07/01 Python
python3:excel操作之读取数据并返回字典 + 写入的案例
2020/09/01 Python
25个CSS3动画按钮和菜单教程分享
2012/10/03 HTML / CSS
CSS3径向渐变之大鱼吃小鱼之孤单的大鱼
2016/04/26 HTML / CSS
html5版canvas自由拼图实例
2014/10/15 HTML / CSS
英国著名国际平价时尚男装品牌:Topman
2016/08/27 全球购物
Lancer Skincare官方网站:抗衰老皮肤护理
2020/11/20 全球购物
优秀毕业自我鉴定
2014/02/15 职场文书
函授本科个人自我鉴定
2014/03/25 职场文书
小班幼儿评语大全
2014/04/30 职场文书
MySQL连表查询分组去重的实现示例
2021/07/01 MySQL