Send me a PHP cache implementation that implements apc and file caching. By inheriting Cache_Abstract, you can call third-party caching tools.
Refer to shindig's cache class and apc.
Php code
-
-
class CacheException extends Exception {}
-
/**
-
* Cache abstract class
-
*/
-
abstract class Cache_Abstract {
-
/**
-
* Read cache variables
-
* *
-
* @param string $key cache index
-
* @return mixed
-
*/
-
abstract public function fetch($key);
-
-
/**
-
* Cache variables
-
* *
-
* @param string $key cache variable subscript
-
* @param string $value The value of the cache variable
-
* @return bool
-
*/
-
abstract public function store($key, $value);
-
-
/**
-
* Delete cache variables
-
* *
-
* @param string $key cache index
-
* @return Cache_Abstract
-
*/
-
abstract public function delete($key);
-
-
/**
-
* Clear (delete) all caches
-
* *
-
* @return Cache_Abstract
-
*/
-
abstract public function clear();
-
-
/**
-
* Lock cache variables
-
* *
-
* @param string $key cache index
-
* @return Cache_Abstract
-
*/
-
abstract public function lock($key);
-
-
/**
-
* Cache variable unlocking
-
* *
-
* @param string $key cache index
-
* @return Cache_Abstract
-
*/
-
abstract public function unlock($key);
-
-
/**
-
* Get whether the cache variable is locked
-
* *
-
* @param string $key cache index
-
* @return bool
-
*/
-
abstract public function isLocked($key);
-
-
/**
-
* Make sure it is not locked
-
* * Sleep at most $tries times to wait for unlocking, skip and unlock when timeout
-
* *
-
* @param string $key cache index
-
*/
-
public function checkLock($key) {
-
If (!$this->isLocked($key)) {
-
Return $this;
} -
-
$tries = 10;
-
$count = 0;
do { -
usleep(200); -
$count ++;
-
-
$this->isLocked($key) && $this->unlock($key); -
-
return $this;
} -
} -
-
-
/**-
* APC extended cache implementation -
* -
* -
* @category Mjie -
* @package Cache -
* @author Liushui Mengchun -
* @copyright Copyright (c) 2008- -
* @license New BSD License -
* @version $Id: Cache/Apc.php version number 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'); -
} -
} -
-
/**-
* * Save cache variables -
* * -
* @param string $key -
* @param mixed $value -
* @return bool -
*/ -
Public function store($key, $value) { -
return apc_store($this->_storageKey($key), $value); -
} -
-
/**-
* Read cache -
* * -
* @param string $key -
* @return mixed -
*/ -
Public function fetch($key) { -
return apc_fetch($this->_storageKey($key)); -
}
-
-
/**
-
* Clear cache
-
* *
-
* @return Cache_Apc
-
*/
-
public function clear() {
-
apc_clear_cache();
-
return $this;
-
}
-
-
/**
-
* Delete cache unit
-
* *
-
* @return Cache_Apc
-
*/
-
public function delete($key) {
-
apc_delete($this->_storageKey($key));
-
return $this;
-
}
-
-
/**
-
* Whether the cache unit is locked
-
* *
-
* @param string $key
-
* @return bool
-
*/
-
public function isLocked($key) {
-
if ((apc_fetch($this->_storageKey($key) . '.lock')) === false) {
-
return false;
-
}
-
-
return true;
-
}
-
-
/**
-
* Lock cache unit
-
* *
-
* @param string $key
-
* @return Cache_Apc
-
*/
-
public function lock($key) {
-
apc_store($this->_storageKey($key) . '.lock', '', 5);
-
return $this;
-
}
-
-
/**
-
* Cache unit unlocked
-
* *
-
* @param string $key
-
* @return Cache_Apc
-
*/
-
public function unlock($key) {
-
apc_delete($this->_storageKey($key) . '.lock');
-
return $this;
-
}
-
-
/**
-
* Full cache name
-
* *
-
* @param string $key
-
* @return string
-
*/
-
private function _storageKey($key) {
-
return $this->_prefix . '_' . $key;
-
}
-
}
-
-
/**
-
* File cache implementation
-
*
-
*
-
* @category Mjie
-
* @package Cache
-
* @author Liushui Mengchun
-
* @copyright Copyright (c) 2008-
-
* @license New BSD License
-
* @version $Id: Cache/File.php version number 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');
-
}
-
}
-
-
/**
-
* Get cache files
-
* *
-
* @param string $key
-
* @return string
-
*/
-
protected function _getCacheFile($key) {
-
$subdir = $this->useSubdir ? substr($key, 0, 2) . '/' : '';
-
return $this->_cachesDir . '/' . $subdir . $key . '.php';
-
}
-
-
/**
-
* Read cache variables
-
* To prevent information leakage, the cache file format is a php file and starts with ""
-
* *
-
* @param string $key cache index
-
* @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;
-
}
-
-
/**
-
* Cache variables
-
* To prevent information leakage, the cache file format is a php file and starts with ""
-
* *
-
* @param string $key cache variable subscript
-
* @param string $value The value of the cache variable
-
* @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, '
-
-
return @file_put_contents($cacheFile, '' . serialize($value));
-
}
-
-
/**
-
* Delete cache variables
-
* *
-
* @param string $key cache index
-
* @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;
-
}
-
-
/**
-
* Whether the cache unit has been locked
-
* *
-
* @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
http://www.bkjia.com/PHPjc/735086.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735086.htmlTechArticleSend a PHP cache implementation, which implements apc and file caching. By inheriting Cache_Abstract, you can call third-party caching tools. . Refer to shindig's cache class and apc. Php code ?php classCacheE...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn