来自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完整的日历类(CLASS)
Nov 27 PHP
基于session_unset与session_destroy的区别详解
Jun 03 PHP
php 模拟post_验证页面的返回状态(实例讲解)
Oct 28 PHP
php define的第二个参数使用方法
Nov 04 PHP
php中的静态变量的基本用法
Mar 20 PHP
PHP使用静态方法的几个注意事项
Sep 16 PHP
Laravel 5框架学习之Eloquent 关系
Apr 09 PHP
PHP、Java des加密解密实例
Apr 27 PHP
PHP中PDO的事务处理分析
Apr 07 PHP
PHP 用session与gd库实现简单验证码生成与验证的类方法
Nov 15 PHP
PHP获取input输入框中的值去数据库比较显示出来
Nov 16 PHP
PHP7扩展开发教程之Hello World实现方法示例
Aug 03 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安全配置方法
2007/06/16 PHP
解析php二分法查找数组是否包含某一元素
2013/05/23 PHP
基于PHP+Ajax实现表单验证的详解
2013/06/25 PHP
ThinkPHP路由详解
2015/07/27 PHP
适合PHP初学者阅读的4本经典书籍
2016/09/23 PHP
Zend Framework框架实现类似Google搜索分页效果
2016/11/25 PHP
javascript中字符串替换函数replace()方法与c# 、vb 替换有一点不同
2010/06/25 Javascript
js 实现图片预加载(js操作 Image对象属性complete ,事件onload 异步加载图片)
2011/03/25 Javascript
jQuery LigerUI 使用教程入门篇
2012/01/18 Javascript
jQuery contains过滤器实现精确匹配使用方法
2013/04/12 Javascript
form表单action提交的js部分与html部分
2014/01/07 Javascript
jquery阻止后续事件只执行第一个事件
2014/07/24 Javascript
JavaScript控制网页平滑滚动到指定元素位置的方法
2015/04/17 Javascript
javascript实现鼠标放上后下边对应内容变换的效果
2015/08/06 Javascript
jQuery插件实现多级联动菜单效果
2015/12/01 Javascript
JS中sort函数排序用法实例分析
2016/06/16 Javascript
详解如何使用vue-cli脚手架搭建Vue.js项目
2017/05/19 Javascript
基于zepto.js实现登录界面
2017/10/09 Javascript
Vue+Jwt+SpringBoot+Ldap完成登录认证的示例代码
2018/05/21 Javascript
javascript和php使用ajax通信传递JSON的实例
2018/08/21 Javascript
vue多层嵌套路由实例分析
2019/03/19 Javascript
python实现自动登录
2018/09/17 Python
DRF跨域后端解决之django-cors-headers的使用
2019/01/27 Python
Python日期格式和字符串格式相互转换的方法
2020/02/18 Python
Python爬虫Scrapy框架CrawlSpider原理及使用案例
2020/11/20 Python
python中lower函数实现方法及用法讲解
2020/12/23 Python
css3绘制百度的小度熊
2018/10/29 HTML / CSS
HTML5 Canvas的常用线条属性值总结
2016/03/17 HTML / CSS
俄罗斯玩具、儿童用品、儿童服装和鞋子网上商店:MyToys.ru
2019/10/14 全球购物
大唐面试试题(CPU,UNIX等等)
2012/01/11 面试题
思想政治自我鉴定
2013/10/06 职场文书
会计辞职信范文
2014/01/15 职场文书
教师个人读书活动总结
2014/07/08 职场文书
婚前财产协议书范本
2014/10/19 职场文书
2015年文员个人工作总结
2015/04/09 职场文书
单位工资证明范本
2015/06/12 职场文书