来自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 相关文章推荐
ADODB的数据库封包程序库
Dec 31 PHP
让你成为更出色的PHP开发者的10个技巧
Feb 25 PHP
php之CodeIgniter学习笔记
Jun 17 PHP
解析PHP生成静态html文件的三种方法
Jun 18 PHP
php实现memcache缓存示例讲解
Dec 04 PHP
二进制交叉权限微型php类分享
Feb 07 PHP
curl和libcurl的区别简介
Jul 01 PHP
php短信接口代码
May 13 PHP
教你在header中隐藏php的版本信息
Aug 10 PHP
thinkphp5.0自定义验证规则使用方法
Nov 16 PHP
关于Yii2框架跑脚本时内存泄漏问题的分析与解决
Dec 01 PHP
基于PHP+mysql实现新闻发布系统的开发
Aug 06 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 ignore_user_abort与register_shutdown_function 使用方法
2009/06/14 PHP
使用迭代器 遍历文件信息的详解
2013/06/08 PHP
解析在apache里面给php写虚拟目录的详细方法
2013/06/24 PHP
解析数组非数字键名引号的必要性
2013/08/09 PHP
PHP中把对象转换为关联数组代码分享
2015/04/09 PHP
PHP实现限制IP访问的方法
2017/04/20 PHP
jQuery版Tab标签切换
2011/03/16 Javascript
映彩衣的js随笔(js图片切换效果)
2011/07/31 Javascript
用js实现in_array的方法
2013/11/05 Javascript
js对象转json数组的简单实现案例
2014/02/28 Javascript
JavaScript sub方法入门实例(把字符串显示为下标)
2014/10/17 Javascript
node.js下when.js 的异步编程实践
2014/12/03 Javascript
理解javascript回调函数
2014/12/28 Javascript
JavaScript内存泄漏的处理方式
2017/11/20 Javascript
用原生 JS 实现 innerHTML 功能实例详解
2019/04/03 Javascript
BootStrap前端框架使用方法详解
2020/02/26 Javascript
[00:03]DOTA2新版本PA至宝展示
2014/11/19 DOTA
[00:34]DOTA2上海特级锦标赛 Spirit战队宣传片
2016/03/04 DOTA
Python中的包和模块实例
2014/11/22 Python
python下载文件时显示下载进度的方法
2015/04/02 Python
Python检测一个对象是否为字符串类的方法
2015/05/21 Python
python 格式化输出百分号的方法
2019/01/20 Python
初探利用Python进行图文识别(OCR)
2019/02/26 Python
python用什么编辑器进行项目开发
2020/06/17 Python
实例教程 HTML5 Canvas 超炫酷烟花绽放动画实现代码
2014/11/05 HTML / CSS
贝玲妃美国官方网站:Benefit美国
2016/08/28 全球购物
全球知名的婚恋交友网站:Match.com
2017/01/05 全球购物
中国双语服务优势的在线购票及活动平台:247tickets
2018/10/26 全球购物
酒店服务与管理毕业生求职信
2013/11/02 职场文书
教师远程培训感言
2014/03/06 职场文书
就业协议书的作用
2014/04/11 职场文书
2014年维修工作总结
2014/11/22 职场文书
商务邀请函
2015/01/30 职场文书
读《人生的智慧》有感:闲暇是人生的精华
2019/12/25 职场文书
Python OpenCV超详细讲解读取图像视频和网络摄像头
2022/04/02 Python
Python 统计序列中元素的出现频度
2022/04/26 Python