天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP 緩存實(shí)現(xiàn)代碼及詳細(xì)注釋

復(fù)制代碼 代碼如下:
class CacheException extends Exception {}
/**
* 緩存抽象類(lèi)
*/
abstract class Cache_Abstract {
/**
* 讀緩存變量
*
* @param string $key 緩存下標(biāo)
* @return mixed
*/
abstract public function fetch($key);

/**
* 緩存變量
*
* @param string $key 緩存變量下標(biāo)
* @param string $value 緩存變量的值
* @return bool
*/
abstract public function store($key, $value);

/**
* 刪除緩存變量
*
* @param string $key 緩存下標(biāo)
* @return Cache_Abstract
*/
abstract public function delete($key);

/**
* 清(刪)除所有緩存
*
* @return Cache_Abstract
*/
abstract public function clear();

/**
* 鎖定緩存變量
*
* @param string $key 緩存下標(biāo)
* @return Cache_Abstract
*/
abstract public function lock($key);
/**
* 緩存變量解鎖
*
* @param string $key 緩存下標(biāo)
* @return Cache_Abstract
*/
abstract public function unlock($key);
/**
* 取得緩存變量是否被鎖定
*
* @param string $key 緩存下標(biāo)
* @return bool
*/
abstract public function isLocked($key);
/**
* 確保不是鎖定狀態(tài)
* 最多做$tries次睡眠等待解鎖,超時(shí)則跳過(guò)并解鎖
*
* @param string $key 緩存下標(biāo)
*/
public function checkLock($key) {
if (!$this->isLocked($key)) {
return $this;
}

$tries = 10;
$count = 0;
do {
usleep(200);
$count ++;
} while ($count <= $tries && $this->isLocked($key)); // 最多做十次睡眠等待解鎖,超時(shí)則跳過(guò)并解鎖
$this->isLocked($key) && $this->unlock($key);

return $this;
}
}

/**
* APC擴(kuò)展緩存實(shí)現(xiàn)
*
*
* @category Mjie
* @package Cache
* @author 流水孟春
* @copyright Copyright (c) 2008- <cmpan(at)qq.com>
* @license New BSD License
* @version $Id: Cache/Apc.php 版本號(hào) 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;
}
}
/**
* 文件緩存實(shí)現(xiàn)
*
*
* @category Mjie
* @package Cache
* @author 流水孟春
* @copyright Copyright (c) 2008- <cmpan(at)qq.com>
* @license New BSD License
* @version $Id: Cache/File.php 版本號(hào) 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;?>"開(kāi)頭
*
* @param string $key 緩存下標(biāo)
* @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;?>"開(kāi)頭
*
* @param string $key 緩存變量下標(biāo)
* @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 緩存下標(biāo)
* @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;
}
/**
* 緩存單元是否已經(jīng)鎖定
*
* @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");
}
}
}
// 設(shè)定緩存鎖文件的訪問(wèn)和修改時(shí)間
@touch($cacheFile . '.lock');
return $this;
}

/**
* 解鎖
*
* @param string $key
* @return Cache_File
*/
public function unlock($key) {
$cacheFile = self::_getCacheFile($key);
@unlink($cacheFile . '.lock');
return $this;
}
/**
* 設(shè)置文件緩存目錄
* @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)) {
// 緩存文件夾有兩級(jí)
$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;
}
}
/**
* 緩存單元的數(shù)據(jù)結(jié)構(gòu)
* array(
* 'time' => time(), // 緩存寫(xiě)入時(shí)的時(shí)間戳
* 'expire' => $expire, // 緩存過(guò)期時(shí)間
* 'valid' => true, // 緩存是否有效
* 'data' => $value // 緩存的值
* );
*/
final class Cache {
/**
* 緩存過(guò)期時(shí)間長(zhǎng)度(s)
*
* @var int
*/
private $_expire = 3600;
/**
* 緩存處理類(lèi)
*
* @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();
}
/**
* 設(shè)置緩存
*
* @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;
}
/**
* 讀緩存,包括過(guò)期的和無(wú)效的,取得完整的存貯結(jié)構(gòu)
*
* @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();
}
/**
* 把緩存設(shè)為無(wú)效
*
* @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;
}
}

/**
* 設(shè)置緩存過(guò)期時(shí)間(s)
*
* @param int $expire
*/
public function setExpire($expire) {
$this->_expire = (int) $expire;
return $this;
}
}

php技術(shù)PHP 緩存實(shí)現(xiàn)代碼及詳細(xì)注釋,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 色中色成人论坛 | 色爰情人网站 | 和尚扒开双腿蹂躏 | 曰本真人00XX动太图 | 美女激清床上戏大全 | 91久久99久91天天拍拍 | 亚洲成熟人网站 | 在线观看国产视频 | 日韩精品一区二区三区色欲AV | 欧美派对xxxhdparty | 亚洲精品色情婷婷在线播放 | 男助理憋尿PLAY灌尿BL出去 | 2020国产成人精品免费视频 | 人妻少妇69式99偷拍 | 性吧 校园春色 | 午夜免费福利小电影 | 97在线视频免费观看97 | 任你躁国语自产二区在线播放 | BL文库好大粗黑强强肉NP | 日本欧美久久久久免费播放网 | 久久久久国产一级毛片高清片 | 国产中文视频无码成人精品 | 国产不卡无码高清视频 | 亚洲色无码播放 | 精品无人区麻豆乱码1区2 | 97在线看视频福利免费 | va亚洲va天堂va视频在线 | 亚洲欧美在无码片一区二区 | 极品美女久久久久久久久久久 | 东京热 百度影音 | 女性BBWBBWBBWBBW| 亚洲国产在线2o20 | 国产 精品 亚洲 欧美 高清 | 国产精品你懂得 | 少妇人妻偷人精品视蜜桃 | 男人吃奶摸下挵进去啪啪 | 宅男午夜大片又黄又爽大片 | 精品无码久久久久久久久 | 孕妇bbwbbwbbwbbw超清 | 99精品国产第一福利网站 | 3D内射动漫同人资源在线观看 |