PHP 缓存实现代码及详细注释
程序员文章站
2023-10-17 17:31:08
复制代码 代码如下:class cacheexception extends exception {} /** * 缓存抽象类 */ abstract class cach...
复制代码 代码如下:
class cacheexception extends exception {}
/**
* 缓存抽象类
*/
abstract class cache_abstract {
/**
* 读缓存变量
*
* @param string $key 缓存下标
* @return mixed
*/
abstract public function fetch($key);
/**
* 缓存变量
*
* @param string $key 缓存变量下标
* @param string $value 缓存变量的值
* @return bool
*/
abstract public function store($key, $value);
/**
* 删除缓存变量
*
* @param string $key 缓存下标
* @return cache_abstract
*/
abstract public function delete($key);
/**
* 清(删)除所有缓存
*
* @return cache_abstract
*/
abstract public function clear();
/**
* 锁定缓存变量
*
* @param string $key 缓存下标
* @return cache_abstract
*/
abstract public function lock($key);
/**
* 缓存变量解锁
*
* @param string $key 缓存下标
* @return cache_abstract
*/
abstract public function unlock($key);
/**
* 取得缓存变量是否被锁定
*
* @param string $key 缓存下标
* @return bool
*/
abstract public function islocked($key);
/**
* 确保不是锁定状态
* 最多做$tries次睡眠等待解锁,超时则跳过并解锁
*
* @param string $key 缓存下标
*/
public function checklock($key) {
if (!$this->islocked($key)) {
return $this;
}
$tries = 10;
$count = 0;
do {
usleep(200);
$count ++;
} while ($count <= $tries && $this->islocked($key)); // 最多做十次睡眠等待解锁,超时则跳过并解锁
$this->islocked($key) && $this->unlock($key);
return $this;
}
}
/**
* apc扩展缓存实现
*
*
* @category mjie
* @package cache
* @author 流水孟春
* @copyright copyright (c) 2008- <cmpan(at)qq.com>
* @license new bsd license
* @version $id: cache/apc.php 版本号 2010-04-18 23:02 cmpan $
*/
class cache_apc extends cache_abstract {
protected $_prefix = 'cache.mjie.net';
public function __construct() {
if (!function_exists('apc_cache_info')) {
throw new cacheexception('apc extension didn\'t installed');
}
}
/**
* 保存缓存变量
*
* @param string $key
* @param mixed $value
* @return bool
*/
public function store($key, $value) {
return apc_store($this->_storagekey($key), $value);
}
/**
* 读取缓存
*
* @param string $key
* @return mixed
*/
public function fetch($key) {
return apc_fetch($this->_storagekey($key));
}
/**
* 清除缓存
*
* @return cache_apc
*/
public function clear() {
apc_clear_cache();
return $this;
}
/**
* 删除缓存单元
*
* @return cache_apc
*/
public function delete($key) {
apc_delete($this->_storagekey($key));
return $this;
}
/**
* 缓存单元是否被锁定
*
* @param string $key
* @return bool
*/
public function islocked($key) {
if ((apc_fetch($this->_storagekey($key) . '.lock')) === false) {
return false;
}
return true;
}
/**
* 锁定缓存单元
*
* @param string $key
* @return cache_apc
*/
public function lock($key) {
apc_store($this->_storagekey($key) . '.lock', '', 5);
return $this;
}
/**
* 缓存单元解锁
*
* @param string $key
* @return cache_apc
*/
public function unlock($key) {
apc_delete($this->_storagekey($key) . '.lock');
return $this;
}
/**
* 完整缓存名
*
* @param string $key
* @return string
*/
private function _storagekey($key) {
return $this->_prefix . '_' . $key;
}
}
/**
* 文件缓存实现
*
*
* @category mjie
* @package cache
* @author 流水孟春
* @copyright copyright (c) 2008- <cmpan(at)qq.com>
* @license new bsd license
* @version $id: cache/file.php 版本号 2010-04-18 16:46 cmpan $
*/
class cache_file extends cache_abstract {
protected $_cachesdir = 'cache';
public function __construct() {
if (defined('data_dir')) {
$this->_setcachedir(data_dir . '/cache');
}
}
/**
* 获取缓存文件
*
* @param string $key
* @return string
*/
protected function _getcachefile($key) {
return $this->_cachesdir . '/' . substr($key, 0, 2) . '/' . $key . '.php';
}
/**
* 读取缓存变量
* 为防止信息泄露,缓存文件格式为php文件,并以"<?php exit;?>"开头
*
* @param string $key 缓存下标
* @return mixed
*/
public function fetch($key) {
$cachefile = self::_getcachefile($key);
if (file_exists($cachefile) && is_readable($cachefile)) {
return unserialize(@file_get_contents($cachefile, false, null, 13));
}
return false;
}
/**
* 缓存变量
* 为防止信息泄露,缓存文件格式为php文件,并以"<?php exit;?>"开头
*
* @param string $key 缓存变量下标
* @param string $value 缓存变量的值
* @return bool
*/
public function store($key, $value) {
$cachefile = self::_getcachefile($key);
$cachedir = dirname($cachefile);
if(!is_dir($cachedir)) {
if(mkdir($cachedir" target="_blank">!@mkdir($cachedir, 0755, true)) {
throw new cacheexception("could not make cache directory");
}
}
return @file_put_contents($cachefile, '<?php exit;?>' . serialize($value));
}
/**
* 删除缓存变量
*
* @param string $key 缓存下标
* @return cache_file
*/
public function delete($key) {
if(emptyempty($key)) {
throw new cacheexception("missing argument 1 for cache_file::delete()");
}
$cachefile = self::_getcachefile($key);
if($cachefile" target="_blank">!@unlink($cachefile)) {
throw new cacheexception("cache file could not be deleted");
}
return $this;
}
/**
* 缓存单元是否已经锁定
*
* @param string $key
* @return bool
*/
public function islocked($key) {
$cachefile = self::_getcachefile($key);
clearstatcache();
return file_exists($cachefile . '.lock');
}
/**
* 锁定
*
* @param string $key
* @return cache_file
*/
public function lock($key) {
$cachefile = self::_getcachefile($key);
$cachedir = dirname($cachefile);
if(!is_dir($cachedir)) {
if(mkdir($cachedir" target="_blank">!@mkdir($cachedir, 0755, true)) {
if(!is_dir($cachedir)) {
throw new cacheexception("could not make cache directory");
}
}
}
// 设定缓存锁文件的访问和修改时间
@touch($cachefile . '.lock');
return $this;
}
/**
* 解锁
*
* @param string $key
* @return cache_file
*/
public function unlock($key) {
$cachefile = self::_getcachefile($key);
@unlink($cachefile . '.lock');
return $this;
}
/**
* 设置文件缓存目录
* @param string $dir
* @return cache_file
*/
protected function _setcachedir($dir) {
$this->_cachesdir = rtrim(str_replace('\\', '/', trim($dir)), '/');
clearstatcache();
if(!is_dir($this->_cachesdir)) {
mkdir($this->_cachesdir, 0755, true);
}
//
return $this;
}
/**
* 清空所有缓存
*
* @return cache_file
*/
public function clear() {
// 遍历目录清除缓存
$cachedir = $this->_cachesdir;
$d = dir($cachedir);
while(false !== ($entry = $d->read())) {
if('.' == $entry[0]) {
continue;
}
$cacheentry = $cachedir . '/' . $entry;
if(is_file($cacheentry)) {
@unlink($cacheentry);
} elseif(is_dir($cacheentry)) {
// 缓存文件夹有两级
$d2 = dir($cacheentry);
while(false !== ($entry = $d2->read())) {
if('.' == $entry[0]) {
continue;
}
$cacheentry .= '/' . $entry;
if(is_file($cacheentry)) {
@unlink($cacheentry);
}
}
$d2->close();
}
}
$d->close();
return $this;
}
}
/**
* 缓存单元的数据结构
* array(
* 'time' => time(), // 缓存写入时的时间戳
* 'expire' => $expire, // 缓存过期时间
* 'valid' => true, // 缓存是否有效
* 'data' => $value // 缓存的值
* );
*/
final class cache {
/**
* 缓存过期时间长度(s)
*
* @var int
*/
private $_expire = 3600;
/**
* 缓存处理类
*
* @var cache_abstract
*/
private $_storage = null;
/**
* @return cache
*/
static public function createcache($cacheclass = 'cache_file') {
return new self($cacheclass);
}
private function __construct($cacheclass) {
$this->_storage = new $cacheclass();
}
/**
* 设置缓存
*
* @param string $key
* @param mixed $value
* @param int $expire
*/
public function set($key, $value, $expire = false) {
if (!$expire) {
$expire = $this->_expire;
}
$this->_storage->checklock($key);
$data = array('time' => time(), 'expire' => $expire, 'valid' => true, 'data' => $value);
$this->_storage->lock($key);
try {
$this->_storage->store($key, $data);
$this->_storage->unlock($key);
} catch (cacheexception $e) {
$this->_storage->unlock($key);
throw $e;
}
}
/**
* 读取缓存
*
* @param string $key
* @return mixed
*/
public function get($key) {
$data = $this->fetch($key);
if ($data && $data['valid'] && !$data['isexpired']) {
return $data['data'];
}
return false;
}
/**
* 读缓存,包括过期的和无效的,取得完整的存贮结构
*
* @param string $key
*/
public function fetch($key) {
$this->_storage->checklock($key);
$data = $this->_storage->fetch($key);
if ($data) {
$data['isexpired'] = (time() - $data['time']) > $data['expire'] ? true : false;
return $data;
}
return false;
}
/**
* 删除缓存
*
* @param string $key
*/
public function delete($key) {
$this->_storage->checklock($key)
->lock($key)
->delete($key)
->unlock($key);
}
public function clear() {
$this->_storage->clear();
}
/**
* 把缓存设为无效
*
* @param string $key
*/
public function setinvalidate($key) {
$this->_storage->checklock($key)
->lock($key);
try {
$data = $this->_storage->fetch($key);
if ($data) {
$data['valid'] = false;
$this->_storage->store($key, $data);
}
$this->_storage->unlock($key);
} catch (cacheexception $e) {
$this->_storage->unlock($key);
throw $e;
}
}
/**
* 设置缓存过期时间(s)
*
* @param int $expire
*/
public function setexpire($expire) {
$this->_expire = (int) $expire;
return $this;
}
}