cf卡bug不掉血不錯誤代碼 PHP 快取實作程式碼及詳細註釋

WBOY
發布: 2016-07-29 08:42:44
原創
2328 人瀏覽過

複製程式碼 程式碼如下:


class CacheException extends Exception {}
/**
* 快取抽象類別
*/
abstract class Cache_Abstract {
/**
* 讀取快取變數
*
* @param string $key 快取下標
* @return mixed
*/
abstract public ftion f.鑰匙);
/**
* 快取變數
*
* @param string $key 快取變數下標
* @param string $value 快取變數的值
* @return bool
*/
抽象公用函式儲存($key, $value);
/**
* 刪除快取變數
*
* @param string $key 快取下標
* @return Cache_Abstract
*/
抽象公用函數delete($key );
/**
* 清(刪除)除所有快取
*
* @return Cache_Abstract
*/
抽象公用函數clear();
/**
* 鎖定快取變數
*
* @param string $key 快取下標
* @return Cache_Abstract
*/
抽象公用函數lock($key);
/ **
* 快取變數解鎖
*
* @param string $key 快取下標
* @return Cache_Abstract
*/
抽象公用函數unlock($key);
/**
* 取得快取變數是否被鎖定
*
* @param string $key 快取下標
* @return bool
*/
抽象公用函數isLocked($key);
/**
* 確保不是鎖定狀態
* 最多做$tries次睡眠等待解鎖,超時則跳過並解鎖
*
* @param string $key 快取下標
*/
public function checkLock($key) {
if (!$this->isLocked($key)) {
return $this;
}
$嘗試= 10;
$計數= 0;
做{
睡覺(200);
$count ++;
} while ($count isLocked($key )); // 最多做十次睡眠等待解鎖,超時則跳過並解鎖
$this->isLocked($key) && $this->unlock($key);
返回$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.mjistructe.net';
protected $_prefix = 'cache.mjistructe.net';
public function __construct() {conjie.net';
if (!function_exists('apc_cache_info')) {
拋出new CacheException('未安裝apc 擴充功能');
}
}
/**
* 儲存快取變數
*
* @param string $key
* @param mixed $value
* @return bool
*/
公用函數儲存($key, $value) {
return apc_store($this->_storageKey($key), $value) {
return apc_store($this->_storageKey($key), $值);
}
/**
* 讀取快取
*
* @param string $key
* @return mixed
*/
public function fetch($key) {
return apc_fetch($this->_storageKey($key));
}
/**
* 清除快取
*
* @return Cache_Apc
*/
公用函數clear() {
apc_clear_cache();
回傳$this;
}
/**
* 刪除快取單元
*
* @return Cache_Apc
*/
public function delete($key) {
apc_delete(this-$-Key> ($key));
回傳$this;
}
/**
* 快取單元是否被鎖定
*
* @param string $key
* @return bool
*/
public function isLocked($key) {
if ((apc_fetch($this- >_storageKey($key) . '.lock') ) === false) {
回傳false;
}
回傳true;
}
/**
* 鎖定快取單元
*
* @param string $key
* @return Cache_Apc
*/
public function lock($key) {
apc_store($this->_storageKey($key) . '.lock', '', 5);
回傳$this;
}
/**
* 快取單元解鎖
*
* @param string $key
* @return Cache_Apc
*/
公用函數解鎖($key) {
apc_delete($this->_storageKey($key) . '.lock');
回傳$this;
}
/**
* 完整快取名稱
*
* @param string $key
* @return string
*/
私有函數_storageKey($key) {
回傳$this->_prefix 。 '_' 。 $鍵;
}
}
/**
* 檔案快取實作
*
*
* @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 {
protected $_cachesDir = 'cache';
public function __con(struct) {
if (define('DATA_DIR')) {
$this->_setCacheDir(DATA_DIR . '/cache');
}
}
/**
* 取得快取檔案
*
* @param string $key
* @return string
*/ 受保護函數_getCacheFile($key) { 回傳$this->_cachesDir 。 '/' 。 substr($key, 0, 2) 。 '/' 。 $鍵。 '.php'; }
/**
* 讀取快取變數
* 為防止資訊洩露,快取文件格式為php文件,並以""開頭
*
* @param string $key 緩存下標
* @return mixed
*/
public function fetch($key) {
$cacheFile = self::_getCacheFile($key);
if (file_exists($cacheFile) && is_read( $cacheFile)) {
return unserialize(@file_get_contents($cacheFile, false, NULL, 13));
}
回傳false;
}
/**
回傳false;
}
/**
回傳false;
}
/**
* 快取變數
* 為防止資訊洩露,快取文件格式為php文件,並以""開頭
*
* @param string $key 快取變數下標
* @param string $value 快取變數的值
* @return bool
*/
公用函式儲存($key, $value) {
$cacheFile = self::_getCacheFile($key);
$cacheDir = 目錄名稱($cacheFile);
if(!is_dir( $cacheDir)) {
if(mkdir($cacheDir" target="_blank">!@mkdir($cacheDir, 0755, true)) {
拋出新的​​CacheException( "無法建立快取目錄") ;
}
}
return @file_put_contents($cacheFile, '' .serialize($value));
/**
* 刪除快取變數
*
* @param string $key 快取下標
* @return Cache_File
*/
public function delete($key) {
if(emptyempty($key)) {
拋出new CacheException("Cache_File::delete 缺少參數1 ()");
}
$cacheFile = self::_getCacheFile($key);
if($cacheFile" target="_blank">!@unlink($cacheFile)) {
throw new CacheException("快取檔案無法刪除");
}
return $this;
/**
* 快取單元是否已鎖定
*
* @param string $key
* @return bool
*/
public function isLocked($key ) {
$cacheFile = self::_getCacheFile($key)
clearstatcache( );
return file_exists($cacheFile .
}
/**
* 鎖定
*
* @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,$cacheDir, 0755, true)) {
if(!is_dir($cacheDir)) {
throw new CacheException("無法建立快取目錄");
}
}
}
/ /設定快取鎖定檔案的存取和修改時間
@touch($cacheFile . '.lock');
回傳$this;
}
/**
* 解鎖
*
* @param string $key
* @return Cache_File
*/
公用函數解鎖($key) {
$cacheFile = self::_getCacheFile($key);
@unlink($cacheFile . '.lock');
回傳$this;
}
/**
* 設定檔案快取目錄
* @param string $dir
* @return Cache_File
*/
受保護函數_setCacheDir($dir) {
$this->_cachesDir = rtrim(str_replace('\', '/', trim( $dir)) , '/');
clearstatcache();
if(!is_dir($this->_cachesDir)) {
mkdir($this->_cachesDir, 0755, true);
}
}
//
回傳$this;
}
/**
* 清空所有快取
*
* @return Cache_File
*/
public function clear() {
///遍歷目錄清除硬碟
$cacheDir = $this->_cachesDir;
$d = dir($cacheDir);
while(false !== ($entry = $d->read())) {
if('.' = = $entry[0]) {
繼續;
}
$cacheEntry = $cacheDir 。 '/' 。 $條目;
if(is_file($cacheEntry)) {
@unlink($cacheEntry);
} elseif(is_dir($cacheEntry)) {
// 快取資料夾有兩級
$d2 = dir($cacheEntry);
while(false !== ($entry = $d2->read())) {
if('.' == $entry[0]) {
繼續;
}
$cacheEntry .= '/' 。 $條目;
if(is_file($cacheEntry)) {
@unlink($cacheEntry);
}
}
$d2->close();
}
}
$d->close();
回傳$this;
}
}
/**
* 快取單元的資料結構
* array(
* 'time' => time(), // 快取寫入時的時間戳記
* 'expire' => $expire, / / 快取過期時間
* 'valid' => true, // 快取是否有效
* 'data' => $value // 快取的值
* );
*/
最終類別快取{
/ **
* 快取過期時間長度(s)
*
* @var int
*/
private $_expire = 3600;
/**
* 快取處理類別
*
* @var Cache_Abstract
*/
private $_storage = null;
/**
* @return 快取
*/
static public function createCache($cacheClass = 'Cache_File') {
return new self($cacheClass);
}
私有函數__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);
嘗試{
$this->_storage->store($key, $data);
$this->_storage->unlock($key);
} catch (CacheException $e) {
$this->_storage->unlock($key);
扔$e;
}
}
/**
* 讀取快取
*
* @param string $key
* @return mixed
*/
public function get($key) {
$data = $this->fetch($key);
if ($data && $data['valid'] && !$data['isExpired']) {
return $data['data'];
}
回傳 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['中間'] ?真:假;
回傳$資料;
}
回傳 false;
}
/**
* 刪除快取
*
* @param string $key
*/
公用函數刪除($key) {
$this->_storage->checkLock($key)
->lock ($key)
->刪除($key)
->解鎖($key) ;
}
公用函數clear() {
$this->_storage->clear();
}
/**
* 把快取設為無效
*
* @param string $key
*/
public function setInvalidate($key) {
$this->_storage->checkLock($key)
->lock ($鍵);
嘗試{
$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);
扔$e;
}
}
/**
* 設定快取過期時間(s)
*
* @param int $expire
*/
public function setExpire($expire) {
$this->_expire = (int) $expire;
回傳$this;
}
}

以上就介紹了cf卡bug不掉血不錯誤代碼PHP存儲實現代碼及詳細註釋,包括cf卡bug不掉血不錯誤代碼方面的內容,希望對PHP教程有興趣的朋友得到幫助。

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!