發個PHP緩存實現,實現了apc和文件緩存,繼承Cache_Abstract即可實現調用第三方的緩存工具。
參考shindig的緩存類和apc。
Php代碼
-
<?php
-
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 {
-
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文件,並以"<?php exit;?>"開頭
-
*
-
* @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文件,並以"<?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, 0755, true)) {
-
throw new CacheException("Could not make cache directory");
-
}
-
}
-
// 用include方式
-
//return @file_put_contents($cacheFile, '<?php return ' . var_export($value, true). ';');
-
-
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(!@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, 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