欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

来自phpguru得Php Cache类源码

程序员文章站 2023-11-03 21:34:10
cache的作用不用说大家都知道咯,这些天也面试了一些人,发现很多人框架用多了,基础都忘记了,你问一些事情,他总是说框架解决了,而根本不明白是怎么回事,所以也提醒大家应该注...
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);
}
}

源码打包下载