APC擴充套件快取實現

2021-06-08 07:38:09 字數 3647 閱讀 7911

發個php快取實現,實現了apc和檔案快取,繼承cache_abstract即可實現呼叫第三方的快取工具。

參考shindig的快取類和apc。

php**

<?php  

class cacheexception extends exception {}  

/*** 快取抽象類

*/abstract

class cache_abstract   

$tries = 10;  

$count = 0;  

do  while ($count

<= $tries && $this->islocked($key));  // 最多做十次睡眠等待解鎖,超時則跳過並解鎖

$this->islocked($key) && $this->unlock($key);  

return

$this;  

}  }  

/*** apc擴充套件快取實現

* * 

* @category   mjie

* @package    cache

* @author     流水孟春

* @license    new bsd license

* @version    $id: cache/apc.php 版本號 2010-04-18 23:02 cmpan $

*/class cache_apc extends cache_abstract   

}  /**

* 儲存快取變數

** @param string $key

* @param mixed $value

* @return bool

*/public

function store($key, $value)   

/*** 讀取快取

** @param string $key

* @return mixed

*/public

function fetch($key)   

/*** 清除快取

** @return cache_apc

*/public

function clear()   

/*** 刪除快取單元

** @return cache_apc

*/public

function

delete($key)   

/*** 快取單元是否被鎖定

** @param string $key

* @return bool

*/public

function islocked($key)   

return true;  

}  /**

* 鎖定快取單元

** @param string $key

* @return cache_apc

*/public

function lock($key)   

/*** 快取單元解鎖

** @param string $key

* @return cache_apc

*/public

function unlock($key)   

/*** 完整快取名

** @param string $key

* @return string

*/private

function _storagekey($key)   

}  /**

* 檔案快取實現

* * 

* @category   mjie

* @package    cache

* @author     流水孟春

* @license    new bsd license

* @version    $id: cache/file.php 版本號 2010-04-18 16:46 cmpan $

*/class cache_file extends cache_abstract   

}  /**

* 獲取快取檔案

** @param string $key

* @return string

*/protected

function _getcachefile($key)   

/*** 讀取快取變數

* 為防止資訊洩露,快取檔案格式為php檔案,並以"<?php exit;?>"開頭

* * @param string $key 快取下標

* @return mixed

*/public

function fetch($key)   

return false;  

}  /**

* 快取變數

* 為防止資訊洩露,快取檔案格式為php檔案,並以"<?php exit;?>"開頭

** @param string $key 快取變數下標

* @param string $value 快取變數的值

* @return bool

*/public

function store($key, $value)   

}  // 用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)   

$cachefile = self::_getcachefile($key);  

if(!@unlink($cachefile))   

return

$this;  

}  /**

* 快取單元是否已經鎖定

** @param string $key

* @return bool

*/public

function islocked($key)   

/*** 鎖定

** @param string $key

* @return cache_file

*/public

function lock($key)   

}  }  

// 設定快取鎖檔案的訪問和修改時間

@touch($cachefile . '.lock');  

return

$this;  

}  /**

* 解鎖

** @param string $key

* @return cache_file

*/public

function unlock($key) {  

$cachefile = self::_getcachefile($key);  

@unlink($cachefile . '.lock');  

return

Redis 擴充套件 快取雪崩和快取穿透問題解決方案

快取同一時間大面積失效,所以,後面的請求都會落到資料庫上,造成資料庫短時間內承受大量的請求而崩掉。解決辦法 快取雪崩解決方案 一般是故意請求快取中不存在的資料,導致所有的請求都落到資料庫上,造成資料庫短時間內承受大量的請求而崩掉。解決辦法 只要用到快取,就可能會涉及到快取與資料庫雙儲存雙寫,只要是雙...

PHP安裝APC擴充套件

2.將安裝包放入使用者目錄tar資料夾下 3.檢視tar資料夾下剛剛放進來的apc 3.1.13.tgz 4.對apc 3.1.13.tgz進行解壓縮 命令 tar zxvf apc 3.1.13.tgz 5.解壓縮完成後,切換路徑到剛剛解壓的資料夾下 命令 cd apc 3.1.13 6.命令 u...

解決「當前擴充套件快取策略沒有進行註冊」的錯誤

造成這個錯誤的原因是在使用 senparc.weixin sdk 或者 co2net 的擴充套件快取的時候,沒有對擴充套件快取策略進行註冊 啟用 解決方案如下 第一步 先公升級到 senparc.weixin 的最新版本 v5.0.11 以上 依賴 co2net v0.1.11 以上 第二步 檢查是...