PHP 的本地檔案快取處理類(非常高效)

來源:互聯網
上載者:User

為了相容伺服器上沒有安裝memcache的記憶體緩衝,專門開發了一個基於PHP5環境的,高效本地檔案快取類。

支援緩衝失效時間的處理,並且非常節省記憶體。支援3個操作set(), get(),del(),詳細使用方式請見備忘中的example

<?php/** * php檔案快取類 FileCache<br/> * @author Jerryli(hzjerry@gmail.com) * @version V0.20130513 * @package * @example * <pre> * $oFC = new CFileCache('./tmp/'); //建立檔案快取類 * $sKey = 'ab_123'; //緩衝索引值 * $data = $oFC->get($sKey); //取得緩衝 * if(is_null($data)) *   $oFC->set($sKey, array('name'=>'ttt', 'datetime'=>date('Y-m-d H:i:s')), 10); //緩衝不存在建立緩衝 * print_r($data); * </pre> */final class CFileCache{/** * 緩衝目錄 * @var string */private static $msCachePath  = null;/** * 預設緩衝失效時間(1小時) * @var int */const miEXPIRE = 3600;/** * 構造<br /> * self::$msCachePath 緩衝目錄為共用目錄 * @param string $sCachePath */function __construct($sCachePath='./tmp/'){if (is_null(self::$msCachePath))self::$msCachePath = $sCachePath;}/** * 讀取緩衝<br /> * 返回: 緩衝內容,字串或數組;緩衝為空白或到期返回null * @param string $sKey 緩衝索引值(無需做md5()) * @return string | null * @access public */public function get($sKey){if(empty($sKey))return false;$sFile  = self::getFileName($sKey);if(!file_exists($sFile))return null;else{$handle = fopen($sFile,'rb');if (intval(fgets($handle)) > time())//檢查時間戳記{//未失效期,取出資料$sData = fread($handle, filesize($sFile));fclose($handle);return unserialize($sData);}else{//已經失效期fclose($handle);return null;}}}/** * 寫入緩衝 * * @param string $sKey 緩衝索引值 * @param mixed $mVal 需要儲存的對象 * @param int $iExpire 失效時間 * @return bool * @access public */public function set($sKey, $mVal, $iExpire=null){if(empty($sKey))return false;$sFile = self::getFileName($sKey);if (!file_exists(dirname($sFile)))if (!self::is_mkdir(dirname($sFile)))return false;$aBuf = array();$aBuf[] = time() + ((empty($iExpire)) ? self::miEXPIRE : intval($iExpire));$aBuf[] = serialize($mVal);/*寫入檔案操作*/$handle = fopen($sFile,'wb');fwrite($handle, implode("\n", $aBuf));fclose($handle);return true;}/** * 刪除指定的緩衝索引值 * * @param string $sKey 緩衝索引值 * @return bool */public function del($sKey){if(empty($sKey))return false;else{@unlink(self::getFileName($sKey));return true;}}/** * 擷取快取檔案全路徑<br /> * 返回: 快取檔案全路徑<br /> * $sKey的值會被轉換成md5(),並分解為3級目錄進行訪問 * @param string $sKey 緩衝鍵 * @return string * @access protected */private static function getFileName($sKey){if(empty($sKey))return false;$key_md5 = md5($sKey);$aFileName = array();$aFileName[]  = rtrim(self::$msCachePath,'/');$aFileName[]  = $key_md5{0} . $key_md5{1};$aFileName[]  = $key_md5{2} . $key_md5{3};$aFileName[]  = $key_md5{4} . $key_md5{5};$aFileName[]  = $key_md5;return implode('/', $aFileName);}/** * 建立目錄<br /> * * @param string $sDir * @return bool */private static function is_mkdir($sDir=''){if(empty($sDir))return false;/*如果無法建立緩衝目錄,讓系統直接拋出錯誤提示*/if(!mkdir($sDir, 0666))return false;elsereturn true;}}?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.