来自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设计模式 Command(命令模式)
Jun 26 PHP
wordpress自定义url参数实现路由功能的代码示例
Nov 28 PHP
两种设置php载入页面时编码的方法
Jul 29 PHP
PHP生成随机数的方法实例分析
Jan 22 PHP
php使用Jpgraph绘制复杂X-Y坐标图的方法
Jun 10 PHP
php使用curl打开https网站的方法
Jun 17 PHP
PHP 二维数组和三维数组的过滤
Mar 16 PHP
利用php输出不同的心形图案
Apr 22 PHP
详解PHP防止盗链防止迅雷下载的方法
Apr 26 PHP
PHP多进程之pcntl_fork的实例详解
Oct 15 PHP
PHP中用Trait封装单例模式的实现
Dec 18 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
PHP入门经历和学习过程分享
2014/04/11 PHP
php支持中文字符串分割的函数
2015/05/28 PHP
如何使用PHP对网站验证码进行破解
2015/09/17 PHP
JavaScript this 深入理解
2009/07/30 Javascript
javascript offsetX与layerX区别
2010/03/12 Javascript
jQuery 打造动态下滑菜单实现说明
2010/04/15 Javascript
关于flash遮盖div浮动层的解决方法
2010/07/17 Javascript
jQuery根据ID获取input、checkbox、radio、select的示例
2014/08/11 Javascript
JavaScript中用于四舍五入的Math.round()方法讲解
2015/06/15 Javascript
javaScript中的原型解析【推荐】
2016/05/05 Javascript
第五篇Bootstrap 排版
2016/06/21 Javascript
JS+CSS3实现超炫的散列画廊特效
2016/07/16 Javascript
JS中的phototype详解
2017/02/04 Javascript
a标签置灰不可点击的实现方法
2017/02/06 Javascript
React props和state属性的具体使用方法
2018/04/12 Javascript
karma+webpack搭建vue单元测试环境的方法示例
2018/05/24 Javascript
js统计页面上每个标签的数量实例代码
2018/05/29 Javascript
JS实现电话号码的字母组合算法示例
2019/02/26 Javascript
vue中实现图片压缩 file文件的方法
2020/05/28 Javascript
Javascript实现贪吃蛇小游戏(含详细注释)
2020/10/23 Javascript
Python Web服务器Tornado使用小结
2014/05/06 Python
Python程序设计入门(5)类的使用简介
2014/06/16 Python
Python中__new__与__init__方法的区别详解
2015/05/04 Python
利用python将图片转换成excel文档格式
2017/12/30 Python
解决pandas无法在pycharm中使用plot()方法显示图像的问题
2018/05/24 Python
python自动化生成IOS的图标
2018/11/13 Python
python实现Flappy Bird源码
2018/12/24 Python
Python利用itchat库向好友或者公众号发消息的实例
2019/02/21 Python
HTML5实现无刷新修改URL的方法
2019/11/14 HTML / CSS
澳大利亚优惠网站:Deals.com.au
2019/07/02 全球购物
高分子材料个人求职信范文
2013/09/25 职场文书
优秀毕业生推荐信
2013/11/02 职场文书
商场经理竞聘演讲稿
2014/01/01 职场文书
乡镇保密工作责任书
2014/07/28 职场文书
出售房屋协议书范本
2014/10/06 职场文书
java协程框架quasar和kotlin中的协程对比分析
2022/02/24 Java/Android