Home > Backend Development > PHP Tutorial > CF card bug, no blood loss, no error code, PHP cache implementation code and detailed comments

CF card bug, no blood loss, no error code, PHP cache implementation code and detailed comments

WBOY
Release: 2016-07-29 08:42:44
Original
2429 people have browsed it

Copy the code The code is as follows:


class CacheException extends Exception {}
/**
* Cache abstract class
*/
abstract class Cache_Abstract {
/**
* Read cache variable
*
* @param string $key cache subscript
* @return mixed
*/
abstract public function fetch($key);
/**
* Cache variable
*
* @param string $key cache variable subscript
* @param string $value cache variable value
* @return bool
*/
abstract public function store($key, $value);
/**
* Delete cache variable
*
* @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 variable
*
* @param string $key cache index
* @return Cache_Abstract
*/
abstract public function lock($key);
/**
* Cache variable unlock
*
* @param string $key cache subscript
* @return Cache_Abstract
*/
abstract public function unlock($key);
/**
* Get whether the cache variable is locked
*
* @param string $key cache subscript
* @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 occurs
*
* @param string $key cache subscript
*/
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 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 unlock
*
* @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 {
protected $_cachesDir = 'cache';
public function __construct() {
if (defined('DATA_DIR')) {
$this->_setCacheDir(DATA_DIR . '/cache');
}
}
/**
* Get cache file
*
* @param string $key
* @return string
*/
protected function _getCacheFile($key) {
return $this->_cachesDir . '/' . substr($key, 0, 2) . '/' . $key . '.php';
}
/**
* Read cache variables
* To prevent information leakage, the cache file format is a php file and starts with ""
*
* @param string $key cache subscript
* @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;
}
/**
* Cache variable
* To prevent information leakage, the cache file format is a php file and starts with ""
*
* @param string $key cache variable subscript
* @param string $value value of cache variable
* @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, '' . serialize($value));
}
/**
* Delete cache variable
*
* @param string $key cache subscript
* @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;
}
/**
* 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" 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;
}
/**
* Set the file cache directory
* @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;
}
/**
* Clear all caches
*
* @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;
}
}
/**
* Data structure of the cache unit
* array(
* 'time' => time(), // The timestamp when the cache is written
* 'expire' => $expire, // Cache expiration time
* 'valid' => true, // Whether the cache is valid
* 'data' => $value // The cached value
* );
*/
final class Cache {
/**
* Cache expiration time length (s)
*
* @var int
*/
private $_expire = 3600;
/**
* Cache processing class
*
* @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();
}
/**
* Set cache
*
* @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;
}
}
/**
* Read cache
*
* @param string $key
* @return mixed
*/
public function get($key) {
$data = $this->fetch($key);
if ($data && $data['valid'] && !$data['isExpired']) {
return $data['data'];
}
return false;
}
/**
* Read the cache, including expired and invalid ones, and obtain the complete storage structure
*
* @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;
}
/**
* Delete cache
*
* @param string $key
*/
public function delete($key) {
$this->_storage->checkLock($key)
->lock($key)
->delete($key)
->unlock($key);
}
public function clear() {
$this->_storage->clear();
}
/**
* Disable cache
*
* @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;
}
}
/**
* Set cache expiration time (s)
*
* @param int $expire
*/
public function setExpire($expire) {
$this->_expire = (int) $expire;
return $this;
}
}

以上就介绍了cf卡bug不掉血不错误代码 PHP 缓存实现代码及详细注释,包括了cf卡bug不掉血不错误代码方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template