来自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之第五天
Oct 09 PHP
PHP原理之异常机制深入分析
Aug 08 PHP
PHP将DateTime对象转化为友好时间显示的实现代码
Sep 20 PHP
is_uploaded_file函数引发的不能上传文件问题
Oct 29 PHP
非常好用的Zend Framework分页类
Jun 25 PHP
PHP原生函数一定好吗?
Dec 08 PHP
网站防止被刷票的一些思路与方法
Jan 08 PHP
什么是PEAR?什么是PECL?PHP中两个容易混淆的概念解释
Jul 01 PHP
php实现贪吃蛇小游戏
Jul 26 PHP
PHP调用微博接口实现微博登录的方法示例
Sep 22 PHP
php根据命令行参数生成配置文件详解
Mar 15 PHP
PHP设计模式(五)适配器模式Adapter实例详解【结构型】
May 02 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
缅甸的咖啡简史
2021/03/04 咖啡文化
PHP实现获取并生成数据库字典的方法
2016/05/04 PHP
PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)实例详解
2018/04/20 PHP
javascript 表单规则集合对象
2009/07/21 Javascript
JS 实现完美include载入实现代码
2010/08/05 Javascript
Node.js中AES加密和其它语言不一致问题解决办法
2014/03/10 Javascript
在Ubuntu系统上安装Ghost博客平台的教程
2015/06/17 Javascript
jQuery实现径向动画菜单效果
2015/07/17 Javascript
js中通过getElementsByName访问name集合对象的方法
2016/10/31 Javascript
用Angular实时获取本地Localstorage数据,实现一个模拟后台数据登入的效果
2016/11/09 Javascript
javascript中递归的两种写法
2017/01/17 Javascript
前端分页功能的实现以及原理(jQuery)
2017/01/22 Javascript
jQuery用户头像裁剪插件cropbox.js使用详解
2017/06/07 jQuery
jQuery实现的滑块滑动导航效果示例
2018/06/04 jQuery
vue2.0获取鼠标位置的方法
2018/09/13 Javascript
微信小程序实现通过js操作wxml的wxss属性示例
2018/12/06 Javascript
微信小程序云开发之使用云函数
2019/05/17 Javascript
微信小程序连接服务器展示MQTT数据信息的实现
2020/07/14 Javascript
[01:14]DOTA2亚洲邀请赛 ShowOpen
2015/02/07 DOTA
[01:02:09]Liquid vs TNC 2019国际邀请赛淘汰赛 胜者组 BO3 第二场 8.21
2020/07/19 DOTA
import的本质解析
2017/10/30 Python
python之matplotlib学习绘制动态更新图实例代码
2018/01/23 Python
详解python中TCP协议中的粘包问题
2019/03/22 Python
GDAL 矢量属性数据修改方式(python)
2020/03/10 Python
python时间序列数据转为timestamp格式的方法
2020/08/03 Python
css3中用animation的steps属性制作帧动画
2019/04/25 HTML / CSS
John Hardy官方网站:手工设计首饰的奢侈品牌
2017/07/05 全球购物
英国电气世界:Electrical World
2019/09/08 全球购物
建议书的格式
2014/05/12 职场文书
竞赛口号大全
2014/06/16 职场文书
入党个人总结范文
2015/03/02 职场文书
2016年党员学习廉政准则心得体会
2016/01/20 职场文书
《日月潭》教学反思
2016/02/20 职场文书
2019年年中职场激励人心语录30条
2019/08/07 职场文书
Python中for后接else的语法使用
2021/05/18 Python
Python实现提取PDF简历信息并存入Excel
2022/04/02 Python