来自phpguru得Php Cache类源码

 更新时间:2010年04月15日 23:33:05   作者:  
群里也有些朋友对基础知识很不屑,总说有能力就可以了,基础知识考不出来什么.对于这样的观点,我一直不苟同.
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 new static 和 new self详解

    PHP new static 和 new self详解

    使用 self:: 或者 __CLASS__ 对当前类的静态引用,取决于定义当前方法所在的类:使用 static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。
    2017-02-02
  • PHP简单选择排序算法实例

    PHP简单选择排序算法实例

    这篇文章主要介绍了PHP简单选择排序算法实例,本文直接给出实现代码,并以类的方式实现,需要的朋友可以参考下
    2015-01-01
  • PHP读取xml方法介绍

    PHP读取xml方法介绍

    在php开发中,我们经常会越到读取xml文件的情况,这里简单总结下一些方法,方便需要的朋友
    2013-01-01
  • php小经验:解析preg_match与preg_match_all 函数

    php小经验:解析preg_match与preg_match_all 函数

    本篇文章是对php中的preg_match函数与preg_match_all函数进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php实现随机显示图片方法汇总

    php实现随机显示图片方法汇总

    本文分享一个php实现的随机显示图片的函数,可以将指定文件夹中存放的图片随机地显示出来。有兴趣的朋友研究下吧。
    2015-05-05
  • PHP中使用php://input处理相同name值的表单数据

    PHP中使用php://input处理相同name值的表单数据

    这篇文章主要介绍了PHP中使用php://input处理相同name值的表单数据,本文是另一种处理相同name值表单数据的方法,文中同时给出另一种方法,需要的朋友可以参考下
    2015-02-02
  • 谈谈PHP连接Access数据库的注意事项

    谈谈PHP连接Access数据库的注意事项

    有的时候需要用php连接access数据库,结果整了半天Access数据库就是连接不上,查找很多资料,以下是些个人经验,希望能给需要连接access 数据的人带来帮助。
    2016-08-08
  • PHP计算个人所得税示例【不使用速算扣除数】

    PHP计算个人所得税示例【不使用速算扣除数】

    这篇文章主要介绍了PHP计算个人所得税,结合实例形式分析了php自定义函数不使用速算扣除数计算个人所得税的相关操作技巧,涉及数组遍历、数值运算的简单使用,需要的朋友可以参考下
    2018-03-03
  • 聊聊PHP中删除字符串的逗号和尾部斜杠的方法

    聊聊PHP中删除字符串的逗号和尾部斜杠的方法

    这篇文章通过两个实例讲解了PHP中删除字符串中的逗号以及尾部斜杠的方法,文中给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值
    2021-09-09
  • PHP实现的登录页面信息提示功能示例

    PHP实现的登录页面信息提示功能示例

    这篇文章主要介绍了PHP实现的登录页面信息提示功能,涉及php表单提交、数据库查询、判断及session数据存储等相关操作技巧,需要的朋友可以参考下
    2017-07-07

最新评论