来自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 相关文章推荐
怎样在UNIX系统下安装php3
Oct 09 PHP
PHP+DBM的同学录程序(1)
Oct 09 PHP
纯php打造的tab选项卡效果代码(不用js)
Dec 29 PHP
LotusPhp笔记之:基于ObjectUtil组件的使用分析
May 06 PHP
解析php curl_setopt 函数的相关应用及介绍
Jun 17 PHP
PHP读取文件内容后清空文件示例代码
Mar 18 PHP
php设置session值和cookies的学习示例
Mar 21 PHP
ThinkPHP控制器里javascript代码不能执行的解决方法
Nov 22 PHP
基于php(Thinkphp)+jquery 实现ajax多选反选不选删除数据功能
Feb 24 PHP
PHP连接sftp并下载文件的方法教程
Aug 26 PHP
php生成word并下载代码实例
Mar 15 PHP
Laravel实现搜索的时候分页并携带参数
Oct 15 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
丧钟首部独立剧集《丧钟:骑士与龙》北美正式开播,场面血腥
2020/04/09 欧美动漫
thinkPHP3.1验证码的简单实现方法
2016/04/22 PHP
简单实现php上传文件功能
2017/09/21 PHP
详解PHP中的外观模式facade pattern
2018/02/05 PHP
PHP+ajax实现获取新闻数据简单示例
2018/05/08 PHP
java解析json方法总结
2019/05/16 PHP
javascript cookie解码函数(兼容ff)
2008/03/17 Javascript
javascript右下角弹层及自动隐藏(自己编写)
2013/11/20 Javascript
JS冒泡事件的快速解决方法
2013/12/16 Javascript
JavaScript函数的4种调用方法详解
2014/04/22 Javascript
window.location的重写及判断location是否被重写
2014/09/04 Javascript
Jquery $.getJSON 在IE下的缓存问题解决方法
2014/10/10 Javascript
给before和after伪元素设置js效果的方法
2015/12/04 Javascript
java中String类型变量的赋值问题介绍
2016/03/23 Javascript
WebApi+Bootstrap+KnockoutJs打造单页面程序
2016/05/16 Javascript
javaScript语法总结
2016/11/25 Javascript
JQuery 动态生成Table表格实例代码
2016/12/02 Javascript
Vue响应式原理Observer、Dep、Watcher理解
2019/06/06 Javascript
Express 配置HTML页面访问的实现
2020/11/01 Javascript
Python浅拷贝与深拷贝用法实例
2015/05/09 Python
python使用循环打印所有三位数水仙花数的实例
2018/11/13 Python
pyenv与virtualenv安装实现python多版本多项目管理
2019/08/17 Python
python 导入数据及作图的实现
2019/12/03 Python
使用python-Jenkins批量创建及修改jobs操作
2020/05/12 Python
Python Matplotlib绘图基础知识代码解析
2020/08/31 Python
如何在Anaconda中打开python自带idle
2020/09/21 Python
Python 实现PS滤镜中的径向模糊特效
2020/12/03 Python
如何在Canvas中添加事件的方法示例
2019/05/21 HTML / CSS
澳大利亚网上买书:Angus & Robertson
2019/07/21 全球购物
酒店工作职员求职简历的自我评价
2013/10/23 职场文书
精彩自我鉴定
2014/01/16 职场文书
党员干部承诺书
2014/03/25 职场文书
2014年幼儿园后勤工作总结
2014/11/10 职场文书
2014年酒店工作总结范文
2014/11/17 职场文书
企业内部管理控制:采购授权审批制度范本
2020/01/19 职场文书
分析SQL窗口函数之取值窗口函数
2022/04/21 Oracle