您好,欢迎访问一九零五行业门户网

php缓存类实现 支持apc和文件缓存

发现一个php缓存实现,实现了apc和文件缓存,继承cache_abstract即可实现调用第三方的缓存工具。参考shindig的缓存类和apc。
代码如下:
islocked($key)) { return $this; } $tries = 10; $count = 0; do { usleep(200); $count++; } while ($count islocked($key));// 最多做十次睡眠等待解锁,超时则跳过并解锁 $this -> islocked($key) && $this -> unlock($key); return $this; }}/** * apc扩展缓存实现 * * * @category mjie * @package cache * @author 流水孟春 * @copyright copyright (c) 2008- * @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- * @license new bsd license * @version $id: cache/file.php 版本号 2010-04-18 16:46 cmpan $ */class cache_file extends cache_abstract { public $usesubdir = false; protected $_cachesdir = 'cache'; public function __construct() { if (defined('data_dir')) { $this -> _setcachedir(data_dir . '/cache'); } } /** * 获取缓存文件 * * @param string $key * @return string */ protected function _getcachefile($key) { $subdir = $this -> usesubdir ? substr($key, 0, 2) . '/' : ''; return $this -> _cachesdir . '/' . $subdir . $key . '.php'; } /** * 读取缓存变量 * 为防止信息泄露,缓存文件格式为php文件,并以开头 * * @param string $key 缓存下标 * @return mixed */ public function fetch($key) { $cachefile = self::_getcachefile($key); if (file_exists($cachefile) && is_readable($cachefile)) { // include 方式 //return include $cachefile; // 系列化方式 return unserialize(@file_get_contents($cachefile, false, null, 13)); } return false; } /** * 缓存变量 * 为防止信息泄露,缓存文件格式为php文件,并以开头 * * @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, 0755, true)) { throw new cacheexception(could not make cache directory); } } // 用include方式 //return @file_put_contents($cachefile, ' _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; }}
其它类似信息

推荐信息