来自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模板页面中分页代码的解析
Feb 06 PHP
php循环语句 for()与foreach()用法区别介绍
Sep 05 PHP
php判断手机访问还是电脑访问示例分享
Jan 20 PHP
ThinkPHP中U方法的使用浅析
Jun 13 PHP
CodeIgniter中实现泛域名解析
Jul 19 PHP
PHP实现视频文件上传完整实例
Aug 28 PHP
php采用curl实现伪造IP来源的方法
Nov 21 PHP
Laravel中Facade的加载过程与原理详解
Sep 22 PHP
Laravel框架路由设置与使用示例
Jun 12 PHP
PHP常见的序列化与反序列化操作实例分析
Oct 28 PHP
Thinkphp 框架扩展之应用模式实现方法分析
Apr 27 PHP
PHP isset()及empty()用法区别详解
Aug 29 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+mysqli使用面向对象方式更新数据库实例
2015/01/29 PHP
利用php输出不同的心形图案
2016/04/22 PHP
PHP封装的MSSql操作类完整实例
2016/05/26 PHP
PHP实现的pdo连接数据库并插入数据功能简单示例
2019/03/30 PHP
Javascript实例教程(19) 使用HoTMetal(1)
2006/12/23 Javascript
IE JS无提示关闭窗口不提示的方法
2010/04/29 Javascript
IE与FireFox中的childNodes区别
2011/10/20 Javascript
在JavaScript中typeof的用途介绍
2013/04/11 Javascript
JS去除右边逗号的简单方法
2013/07/03 Javascript
js open() 与showModalDialog()方法使用介绍
2013/09/10 Javascript
jquery获取一个元素下面相同子元素的个数代码
2014/07/31 Javascript
微信JS接口汇总及使用详解
2015/01/09 Javascript
javascript实现下班倒计时效果的方法(可桌面通知)
2015/07/10 Javascript
AngularJS 路由和模板实例及路由地址简化方法(必看)
2016/06/24 Javascript
获取JS中网页各种高宽与位置的方法总结
2016/07/27 Javascript
AngularJS入门教程之ng-checked 指令详解
2016/08/01 Javascript
详解webpack运行Babel教程
2018/06/13 Javascript
Vue+axios实现统一接口管理的方法
2018/07/23 Javascript
JavaScript实现简单的弹窗效果
2020/05/19 Javascript
python实现批量下载新浪博客的方法
2015/06/15 Python
Python爬取某平台短视频的方法
2021/02/08 Python
CSS3中31种选择器使用方法教程
2013/12/05 HTML / CSS
使用HTML5里的classList操作CSS类
2016/06/28 HTML / CSS
End Clothing美国站:英国男士潮牌商城
2018/04/20 全球购物
EJB3.1都有哪些改进
2012/11/17 面试题
国贸类专业毕业生的求职信分享
2013/12/08 职场文书
运动会稿件100字
2014/02/21 职场文书
学生党员的自我评价范文
2014/03/01 职场文书
实习公司领导推荐函
2014/05/21 职场文书
奥运会口号
2014/06/13 职场文书
2015年生产部工作总结范文
2015/05/25 职场文书
养成教育主题班会
2015/08/13 职场文书
银行工作心得体会范文
2016/01/23 职场文书
创业计划书之家政服务
2019/09/18 职场文书
springboot利用redis、Redisson处理并发问题的操作
2021/06/18 Java/Android
Oracle 触发器trigger使用案例
2022/02/24 Oracle