php檔案快取方法總結,
為大家分享很全的php檔案快取,供大家參考,具體內容如下
<?php class cache{ private static $_instance = null; protected $_options = array( 'cache_dir' => "./", 'file_name_prefix' => 'cache', 'mode' => '1', //mode 1 為serialize model 2為儲存為可執行檔 ); /** * 得到本類執行個體 * * @return Ambiguous */ public static function getInstance() { if(self::$_instance === null) { self::$_instance = new self(); } return self::$_instance; } /** * 得到緩衝資訊 * * @param string $id * @return boolean|array */ public static function get($id) { $instance = self::getInstance(); //快取檔案不存在 if(!$instance->has($id)) { return false; } $file = $instance->_file($id); $data = $instance->_fileGetContents($file); if($data['expire'] == 0 || time() < $data['expire']) { return $data['contents']; } return false; } /** * 設定一個緩衝 * * @param string $id 緩衝id * @param array $data 緩衝內容 * @param int $cacheLife 緩衝生命 預設為0無限生命 */ public static function set($id, $data, $cacheLife = 0) { $instance = self::getInstance(); $time = time(); $cache = array(); $cache['contents'] = $data; $cache['expire'] = $cacheLife === 0 ? 0 : $time + $cacheLife; $cache['mtime'] = $time; $file = $instance->_file($id); return $instance->_filePutContents($file, $cache); } /** * 清除一條緩衝 * * @param string cache id * @return void */ public static function delete($id) { $instance = self::getInstance(); if(!$instance->has($id)) { return false; } $file = $instance->_file($id); //刪除該緩衝 return unlink($file); } /** * 判斷緩衝是否存在 * * @param string $id cache_id * @return boolean true 緩衝存在 false 緩衝不存在 */ public static function has($id) { $instance = self::getInstance(); $file = $instance->_file($id); if(!is_file($file)) { return false; } return true; } /** * 通過緩衝id得到緩衝資訊路徑 * @param string $id * @return string 快取檔案路徑 */ protected function _file($id) { $instance = self::getInstance(); $fileNmae = $instance->_idToFileName($id); return $instance->_options['cache_dir'] . $fileNmae; } /** * 通過id得到緩衝資訊隱藏檔名 * * @param $id * @return string 快取檔案名 */ protected function _idToFileName($id) { $instance = self::getInstance(); $prefix = $instance->_options['file_name_prefix']; return $prefix . '---' . $id; } /** * 通過filename得到緩衝id * * @param $id * @return string 緩衝id */ protected function _fileNameToId($fileName) { $instance = self::getInstance(); $prefix = $instance->_options['file_name_prefix']; return preg_replace('/^' . $prefix . '---(.*)$/', '$1', $fileName); } /** * 把資料寫入檔案 * * @param string $file 檔案名稱 * @param array $contents 資料內容 * @return bool */ protected function _filePutContents($file, $contents) { if($this->_options['mode'] == 1) { $contents = serialize($contents); } else { $time = time(); $contents = "<?php\n". " // mktime: ". $time. "\n". " return ". var_export($contents, true). "\n?>"; } $result = false; $f = @fopen($file, 'w'); if ($f) { @flock($f, LOCK_EX); fseek($f, 0); ftruncate($f, 0); $tmp = @fwrite($f, $contents); if (!($tmp === false)) { $result = true; } @fclose($f); } @chmod($file,0777); return $result; } /** * 從檔案得到資料 * * @param sring $file * @return boolean|array */ protected function _fileGetContents($file) { if(!is_file($file)) { return false; } if($this->_options['mode'] == 1) { $f = @fopen($file, 'r'); @$data = fread($f,filesize($file)); @fclose($f); return unserialize($data); } else { return include $file; } } /** * 建構函式 */ protected function __construct() { } /** * 設定緩衝路徑 * * @param string $path * @return self */ public static function setCacheDir($path) { $instance = self::getInstance(); if (!is_dir($path)) { exit('file_cache: ' . $path.' 不是一個有效路徑 '); } if (!is_writable($path)) { exit('file_cache: 路徑 "'.$path.'" 不可寫'); } $path = rtrim($path,'/') . '/'; $instance->_options['cache_dir'] = $path; return $instance; } /** * 設定快取檔案首碼 * * @param srting $prefix * @return self */ public static function setCachePrefix($prefix) { $instance = self::getInstance(); $instance->_options['file_name_prefix'] = $prefix; return $instance; } /** * 設定緩衝儲存類型 * * @param int $mode * @return self */ public static function setCacheMode($mode = 1) { $instance = self::getInstance(); if($mode == 1) { $instance->_options['mode'] = 1; } else { $instance->_options['mode'] = 2; } return $instance; } /** * 刪除所有緩衝 * @return boolean */ public static function flush() { $instance = self::getInstance(); $glob = @glob($instance->_options['cache_dir'] . $instance->_options['file_name_prefix'] . '--*'); if(empty($glob)) { return false; } foreach ($glob as $v) { $fileName = basename($v); $id = $instance->_fileNameToId($fileName); $instance->delete($id); } return true; }}/* 初始化設定cache的配置資訊什麼的 */cache::setCachePrefix('core'); //設定快取檔案首碼cache::setCacheDir('./cache'); //設定存放快取檔案夾路徑//模式1 緩衝儲存方式//a:3:{s:8:"contents";a:7:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:34;i:4;i:5;i:5;i:6;i:6;i:6;}s:6:"expire";i:0;s:5:"mtime";i:1318218422;}//模式2 緩衝儲存方式/* <?php // mktime: 1318224645 return array ( 'contents' => array ( 0 => 1, 1 => 2, 2 => 3, 3 => 34, 4 => 5, 5 => 6, 6 => 6, ), 'expire' => 0, 'mtime' => 1318224645,)?> * * */cache::setCacheMode('2');if(!$row = cache::get('zj2')){ $array = array(1,2,3,34,5,6,6); $row = cache::set('zj2',$array);}// cache::flush(); 清空所有緩衝print_r($row);
檔案快取class
<?php/** * 檔案快取類 * @author xiaojiong & 290747680@qq.com * @date 2011-08-17 */class cache{ const FILE_LIFE_KEY = 'FILE_LIFE_KEY'; const CLEAR_ALL_KEY = 'CLEAR_ALL'; static $_instance = null; protected $_options = array( 'cache_dir' => './cache', 'file_locking' => true, 'file_name_prefix' => 'cache', 'cache_file_umask' => 0777, 'file_life' => 100000 ); static function &getInstance($options = array()) { if(self::$_instance === null) { self::$_instance = new self($options); } return self::$_instance; } /** * 設定參數 * @param array $options 緩衝參數 * @return void */ static function &setOptions($options = array()) { return self::getInstance($options); } /** * 建構函式 * @param array $options 緩衝參數 * @return void */ private function __construct($options = array()) { if ($this->_options['cache_dir'] !== null) { $dir = rtrim($this->_options['cache_dir'],'/') . '/'; $this->_options['cache_dir'] = $dir; if (!is_dir($this->_options['cache_dir'])) { mkdir($this->_options['cache_dir'],0777,TRUE); } if (!is_writable($this->_options['cache_dir'])) { exit('file_cache: 路徑 "'. $this->_options['cache_dir'] .'" 不可寫'); } } else { exit('file_cache: "options" cache_dir 不可為空 '); } } /** * 設定緩衝路徑 * @param string $value * @return void */ static function setCacheDir($value) { $self = & self::getInstance(); if (!is_dir($value)) { exit('file_cache: ' . $value.' 不是一個有效路徑 '); } if (!is_writable($value)) { exit('file_cache: 路徑 "'.$value.'" 不可寫'); } $value = rtrim($this->_options['cache_dir'],'/') . '/'; $self->_options['cache_dir'] = $value; } /** * 存入快取資料 * @param array $data 放入緩衝的資料 * @param string $id 緩衝id(又名緩衝識別碼) * @param cache_life 緩衝時間 * @return boolean True if no problem */ static function save($data, $id = null, $cache_life = null) { $self = & self::getInstance(); if (!$id) { if ($self->_id) { $id = $self->_id; } else { exit('file_cache:save() id 不可為空!'); } } $time = time(); if($cache_life) { $data[self::FILE_LIFE_KEY] = $time + $cache_life; } elseif ($cache_life != 0){ $data[self::FILE_LIFE_KEY] = $time + $self->_options['file_life']; } $file = $self->_file($id); $data = "<?php\n". " // mktime: ". $time. "\n". " return ". var_export($data, true). "\n?>" ; $res = $self->_filePutContents($file, $data); return $res; } /** * 得到緩衝資訊 * * @param string $id 緩衝id * @return string|array 快取資料 */ static function load($id) { $self = & self::getInstance(); $time = time(); //檢測緩衝是否存在 if (!$self->test($id)) { // The cache is not hit ! return false; } //全部清空識別檔案 $clearFile = $self->_file(self::CLEAR_ALL_KEY); $file = $self->_file($id); //判斷緩衝是否已被全部清除 if(is_file($clearFile) && filemtime($clearFile) > filemtime($file)) { return false; } $data = $self->_fileGetContents($file); if(empty($data[self::FILE_LIFE_KEY]) || $time < $data[self::FILE_LIFE_KEY]) { unset($data[self::FILE_LIFE_KEY]); return $data; } return false; } /** * 寫入快取檔案 * * @param string $file 緩衝路徑 * @param string $string 緩衝資訊 * @return boolean true 成功 */ protected function _filePutContents($file, $string) { $self = & self::getInstance(); $result = false; $f = @fopen($file, 'ab+'); if ($f) { if ($self->_options['file_locking']) @flock($f, LOCK_EX); fseek($f, 0); ftruncate($f, 0); $tmp = @fwrite($f, $string); if (!($tmp === false)) { $result = true; } @fclose($f); } @chmod($file, $self->_options['cache_file_umask']); return $result; } /** * 格式化後的快取檔案路徑 * * @param string $id 緩衝id * @return string 快取檔案名(包括路徑) */ protected function _file($id) { $self = & self::getInstance(); $fileName = $self->_idToFileName($id); return $self->_options['cache_dir'] . $fileName; } /** * 格式化後的快取檔案名字 * * @param string $id 緩衝id * @return string 快取檔案名 */ protected function _idToFileName($id) { $self = & self::getInstance(); $self->_id = $id; $prefix = $self->_options['file_name_prefix']; $result = $prefix . '---' . $id; return $result; } /** * 判斷緩衝是否存在 * * @param string $id Cache id * @return boolean True 緩衝存在 False 緩衝不存在 */ static function test($id) { $self = & self::getInstance(); $file = $self->_file($id); if (!is_file($file)) { return false; } return true; } /** * 得到緩衝資訊 * * @param string $file 緩衝路徑 * @return string 緩衝內容 */ protected function _fileGetContents($file) { if (!is_file($file)) { return false; } return include $file; } /** * 清除所有緩衝 * * @return void */ static function clear() { $self = & self::getInstance(); $self->save('CLEAR_ALL',self::CLEAR_ALL_KEY); } /** * 清除一條緩衝 * * @param string cache id * @return void */ static function del($id) { $self = & self::getInstance(); if(!$self->test($id)){ // 該緩衝不存在 return false; } $file = $self->_file($id); return unlink($file); } }
存入資料
<?php$config = array( 'name' => 'xiaojiong', 'qq' => '290747680', 'age' => '20',);//第一個參數 緩衝data//第二個參數 緩衝id//第三個參數 cache_life 0 永不到期(cache::clear()清空所有除外) 預設cache_life 為option_cache_lifecache::save($config,'config',0);
載入資料
<?php//只有一個參數 cache_id$config = cache::load('config');
清空緩衝
<?php//清空指定緩衝cache::del('config');//清空所有緩衝cache::clear();
cache資訊配置
//在執行所有cache_func前調用$_options = array( 'cache_dir' => './cache', //快取檔案目錄 'file_name_prefix' => 'cache',//快取檔案首碼 'file_life' => 100000, //快取檔案生命);cache::setOptions($options);//再執行 就會按著新配置資訊執行,否則是預設資訊cache::save($arr,'arr');//就是這個方法 貌似不合理 望大家指點
以上就是本文的全部內容,希望對大家的學習有所協助。
您可能感興趣的文章:
- PHP Memcached + APC + 檔案快取封裝實現代碼
- PHP 檔案快取的效能測試
- php 檔案快取函數
- PHP中檔案快取轉記憶體緩衝的方法
- PHP檔案快取內容儲存格式執行個體分析
- php檔案快取類匯總
- 常見php資料檔案緩衝類匯總
- PHP檔案快取類樣本分享
- php檔案快取類用法執行個體分析
- ThinkPHP檔案快取類代碼分享
http://www.bkjia.com/PHPjc/1111339.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1111339.htmlTechArticlephp檔案快取方法總結, 為大家分享很全的php檔案快取,供大家參考,具體內容如下 php class cache{ private static $_instance = null; protected $_options...