来自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 相关文章推荐
基于mysql的论坛(7)
Oct 09 PHP
PHP安装攻略:常见问题解答(一)
Oct 09 PHP
常用表单验证类,有了这个,一般的验证就都齐了。
Dec 06 PHP
php 变量定义方法
Jun 14 PHP
php面向对象全攻略 (四)构造方法与析构方法
Sep 30 PHP
DISCUZ在win2003环境下 Unable to access ./include/common.inc.php in... 的问题终极解决方案
Nov 21 PHP
PHP无限分类(树形类)
Sep 28 PHP
php获得用户ip地址的比较不错的方法
Feb 08 PHP
ThinkPHP做文字水印时提示call an undefined function exif_imagetype()解决方法
Oct 30 PHP
php+ajax实现无刷新数据分页的办法
Nov 02 PHP
PHP设计模式之装饰器模式定义与用法详解
Apr 02 PHP
php让json_encode不自动转义斜杠“/”的方法
Apr 27 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 URL编码解码函数代码
2009/03/10 PHP
php实现用于删除整个目录的递归函数
2015/03/16 PHP
php实现Mongodb自定义方式生成自增ID的方法
2015/03/23 PHP
图片按比例缩放函数
2006/06/26 Javascript
添加JavaScript重载函数的辅助方法2
2010/07/04 Javascript
jquery获取iframe中的dom对象(两种方法)
2013/07/02 Javascript
Javascript前端UI框架Kit使用指南之Kitjs简介
2014/11/28 Javascript
AngularJS指令详解及示例代码
2016/08/16 Javascript
Javascript将JSON日期格式化
2016/08/23 Javascript
Element-ui自定义table表头、修改列标题样式、添加tooltip、:render-header使用
2019/04/11 Javascript
webpack4 配置 ssr 环境遇到“document is not defined”
2019/10/24 Javascript
javascript实现拖拽碰撞检测
2020/03/12 Javascript
Electron 打包问题:electron-builder 下载各种依赖出错(推荐)
2020/07/09 Javascript
Python实现基于HTTP文件传输实例
2014/11/08 Python
Python进程通信之匿名管道实例讲解
2015/04/11 Python
python查询sqlite数据表的方法
2015/05/08 Python
Python实现PS滤镜碎片特效功能示例
2018/01/24 Python
Python 查看list中是否含有某元素的方法
2018/06/27 Python
对PyQt5基本窗口控件 QMainWindow的使用详解
2019/06/19 Python
python图形用户接口实例详解
2019/12/16 Python
python GUI库图形界面开发之PyQt5切换按钮控件QPushButton详细使用方法与实例
2020/02/28 Python
python sitk.show()与imageJ结合使用常见的问题
2020/04/20 Python
Python基于Webhook实现github自动化部署
2020/11/28 Python
详解matplotlib中pyplot和面向对象两种绘图模式之间的关系
2021/01/22 Python
你的自行车健身专家:FaFit24
2016/11/16 全球购物
windeln官方海外旗舰店:德淘超人气母婴超市
2017/12/15 全球购物
nohup的用法
2012/11/26 面试题
优纳科技软件测试面试题
2012/05/15 面试题
《一株紫丁香》教学反思
2014/02/19 职场文书
后勤部经理岗位职责
2014/02/23 职场文书
美容院合作经营协议书
2014/10/10 职场文书
质量负责人岗位职责
2015/02/15 职场文书
结婚幸福感言
2015/08/01 职场文书
2019年农民幸福观调查的实践感悟
2019/12/19 职场文书
Python生成九宫格图片的示例代码
2021/04/14 Python
mongodb清除连接和日志的正确方法分享
2021/09/15 MongoDB