緩存類是我們開(kāi)發(fā)應(yīng)用中會(huì)常用使用到的功能,下面我來(lái)給大家整理幾個(gè)php文件緩存類了,各個(gè)文件緩存類寫法不同,但在性能上會(huì)有區(qū)別,有興趣測(cè)試的朋友可測(cè)試一下這些緩存類吧.
例1,代碼如下:
- <?php
- $fzz = new fzz_cache;
- $fzz->kk = $_SERVER; //寫入緩存
- //$fzz->set("kk",$_SERVER,10000); //此方法不與類屬性想沖突,可以用任意緩存名;
- print_r($fzz->kk); //讀取緩存
- //print_r($fzz->get("kk"));
- //unset($fzz->kk); //刪除緩存
- //$fzz->_unset("kk");
- var_dump(isset($fzz->kk)); //判斷緩存是否存在
- //$fzz->_isset("kk");
- //$fzz->clear(); //清理過(guò)期緩存
- //$fzz->clear_all(); //清理所有緩存文件
- class fzz_cache{
- public $limit_time = 20000; //緩存過(guò)期時(shí)間
- public $cache_dir = "data"; //緩存文件保存目錄
- //寫入緩存
- function __set($key , $val){
- $this->_set($key ,$val);
- }
- //第三個(gè)參數(shù)為過(guò)期時(shí)間
- function _set($key ,$val,$limit_time=null){
- $limit_time = $limit_time ? $limit_time : $this->limit_time;
- $file = $this->cache_dir."/".$key.".cache";
- $val = serialize($val);
- @file_put_contents($file,$val) or $this->error(__line__,"fail to write in file");
- @chmod($file,0777);
- @touch($file,time()+$limit_time) or $this->error(__line__,"fail to change time");
- }
- //讀取緩存
- function __get($key){
- return $this->_get($key);
- }
- function _get($key){
- $file = $this->cache_dir."/".$key.".cache";
- if (@filemtime($file)>=time()){
- return unserialize(file_get_contents($file));
- }else{
- @unlink($file) or $this->error(__line__,"fail to unlink");
- return false;
- }
- }
- //刪除緩存文件
- function __unset($key){
- return $this->_unset($key);
- }
- function _unset($key){
- if (@unlink($this->cache_dir."/".$key.".cache")){
- return true;
- }else{
- return false;
- }
- }
- //檢查緩存是否存在,過(guò)期則認(rèn)為不存在
- function __isset($key){
- return $this->_isset($key);
- }
- function _isset($key){
- $file = $this->cache_dir."/".$key.".cache";
- if (@filemtime($file)>=time()){
- return true;
- }else{
- @unlink($file) ;
- return false;
- }
- }
- //清除過(guò)期緩存文件
- function clear(){
- $files = scandir($this->cache_dir);
- foreach ($files as $val){
- if (filemtime($this->cache_dir."/".$val)<time()){
- @unlink($this->cache_dir."/".$val);
- }
- }
- }
- //清除所有緩存文件
- function clear_all(){
- $files = scandir($this->cache_dir);
- foreach ($files as $val){
- @unlink($this->cache_dir."/".$val);
- }
- }
- function error($msg,$debug = false) {
- $err = new Exception($msg);
- $str = "<pre>
- <span style='color:red'>error:</span>
- ".print_r($err->getTrace(),1)."
- </pre>";//開(kāi)源代碼Vevb.com
- if($debug == true) {
- file_put_contents(date('Y-m-d H_i_s').".log",$str);
- return $str;
- }else{
- die($str);
- }
- }
- }
- ?>
例2.從CI社區(qū)的stblog和CI的file_helper類中提取出來(lái)的php文件緩存類,一個(gè)簡(jiǎn)單的基于文件的key->value緩存類。
這個(gè)類可以用來(lái)緩存一些基本信息,比如博客的header,footer,sidebar中的一些不經(jīng)常變化,從數(shù)據(jù)庫(kù)中取出的內(nèi)容,取數(shù)據(jù)前先判斷文件緩存中的內(nèi)容是否過(guò)期,如果沒(méi)過(guò)期取出來(lái),過(guò)期了則連接數(shù)據(jù)庫(kù)查詢,并將結(jié)果重新寫入文件緩存,更新過(guò)期時(shí)間。跟memcache使用類似,不過(guò)更方便。用在一些小的應(yīng)用上足夠了...代碼如下:
- <?php
- define('DIRECTORY_SEPARATOR','/');
- define('FOPEN_WRITE_CREATE_DESTRUCTIVE','wb');
- define('FOPEN_WRITE_CREATE','ab');
- define('DIR_WRITE_MODE', 0777);
- class FileCache {
- /**
- * 緩存路徑
- *
- * @access private
- * @var string
- */
- private $_cache_path;
- /**
- * 緩存過(guò)期時(shí)間,單位是秒second
- *
- * @access private
- * @var int
- */
- private $_cache_expire;
- /**
- * 解析函數(shù),設(shè)置緩存過(guò)期實(shí)踐和存儲(chǔ)路徑
- *
- * @access public
- * @return void
- */
- public function __construct($expire, $cache_path)
- {
- $this->_cache_expire = $expire;
- $this->_cache_path = $cache_path;
- }
- /**
- * 緩存文件名
- *
- * @access public
- * @param string $key
- * @return void
- */
- private function _file($key)
- {
- return $this->_cache_path . md5($key);
- }
- /**
- * 設(shè)置緩存
- *
- * @access public
- * @param string $key 緩存的唯一鍵
- * @param string $data 緩存的內(nèi)容
- * @return bool
- */
- public function set($key, $data)
- {
- $value = serialize($data);
- $file = $this->_file($key);
- return $this->write_file($file, $value);
- }
- /**
- * 獲取緩存
- *
- * @access public
- * @param string $key 緩存的唯一鍵
- * @return mixed
- */
- public function get($key)
- {
- $file = $this->_file($key);
- /** 文件不存在或目錄不可寫 */
- if (!file_exists($file) || !$this->is_really_writable($file))
- {
- return false;
- }
- /** 緩存沒(méi)有過(guò)期,仍然可用 */
- if ( time() < (filemtime($file) + $this->_cache_expire) )
- {
- $data = $this->read_file($file);
- if(FALSE !== $data)
- {
- return unserialize($data);
- }
- return FALSE;
- }
- /** 緩存過(guò)期,刪除之 */
- @unlink($file);
- return FALSE;
- }
- function read_file($file)
- {
- if ( ! file_exists($file))
- {
- return FALSE;
- }
- if (function_exists('file_get_contents'))
- {
- return file_get_contents($file);
- }
- if ( ! $fp = @fopen($file, FOPEN_READ))
- {
- return FALSE;
- }
- flock($fp, LOCK_SH);//讀取之前加上共享鎖
- $data = '';
- if (filesize($file) > 0)
- {
- $data =& fread($fp, filesize($file));
- }
- flock($fp, LOCK_UN);//釋放鎖
- fclose($fp);
- return $data;
- }
- function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
- {
- if ( ! $fp = @fopen($path, $mode))
- {
- return FALSE;
- }
- flock($fp, LOCK_EX);
- fwrite($fp, $data);
- flock($fp, LOCK_UN);
- fclose($fp);
- return TRUE;
- }
- function is_really_writable($file)//兼容各平臺(tái)判斷文件是否有寫入權(quán)限
- {
- // If we're on a Unix server with safe_mode off we call is_writable
- if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
- {
- return is_writable($file);
- }
- // For windows servers and safe_mode "on" installations we'll actually
- // write a file then read it. Bah...
- if (is_dir($file))
- {
- $file = rtrim($file, '/').'/'.md5(rand(1,100));
- if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
- {
- return FALSE;
- }
- fclose($fp);
- @chmod($file, DIR_WRITE_MODE);
- @unlink($file);
- return TRUE;
- }
- elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
- {//開(kāi)源代碼Vevb.com
- return FALSE;
- }
- fclose($fp);
- return TRUE;
- }
- }
- $cache = new FileCache(30,'cache/');
- $cache->set('test','this is a test.');
- print $cache->get('test');
- /* End of file FlieCache.php */
- ?>
例3.自己覺(jué)得很好用的php文件緩存,代碼如下:
- <?php
- class cache
- {
- private static $_instance = null;
- protected $_options = array(
- 'cache_dir' => "./",
- 'file_name_prefix' => 'cache',
- 'mode' => '1', //mode 1 為serialize model 2為保存為可執(zhí)行文件
- );
- /**
- * 得到本類實(shí)例
- *
- * @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;
- }
- /**
- * 設(shè)置一個(gè)緩存
- *
- * @param string $id 緩存id
- * @param array $data 緩存內(nèi)容
- * @param int $cacheLife 緩存生命 默認(rèn)為0無(wú)限生命
- */
- 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;
- }
- /**
- * 通過(guò)緩存id得到緩存信息路徑
- * @param string $id
- * @return string 緩存文件路徑
- */
- protected function _file($id)
- {
- $instance = self::getInstance();
- $fileNmae = $instance->_idToFileName($id);
- return $instance->_options['cache_dir'] . $fileNmae;
- }
- /**
- * 通過(guò)id得到緩存信息存儲(chǔ)文件名
- *
- * @param $id
- * @return string 緩存文件名
- */
- protected function _idToFileName($id)
- {
- $instance = self::getInstance();
- $prefix = $instance->_options['file_name_prefix'];
- return $prefix . '---' . $id;
- }
- /**
- * 通過(guò)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);
- }
- /**
- * 把數(shù)據(jù)寫入文件
- *
- * @param string $file 文件名稱
- * @param array $contents 數(shù)據(jù)內(nèi)容
- * @return bool
- */
- protected function _filePutContents($file, $contents)
- {
- if($this->_options['mode'] == 1)
- {
- $contents = serialize($contents);
- }
- else
- {
- $time = time();
- $contents = "<?phpn".
- " // 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;
- }
- /**
- * 從文件得到數(shù)據(jù)
- *
- * @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;
- }
- }
- /**
- * 構(gòu)造函數(shù)
- */
- protected function __construct()
- {
- }
- /**
- * 設(shè)置緩存路徑
- *
- * @param string $path
- * @return self
- */
- public static function setCacheDir($path)
- {
- $instance = self::getInstance();
- if (!is_dir($path)) {
- exit('file_cache: ' . $path.' 不是一個(gè)有效路徑 ');
- }
- if (!is_writable($path)) {
- exit('file_cache: 路徑 "'.$path.'" 不可寫');
- }
- $path = rtrim($path,'/') . '/';
- $instance->_options['cache_dir'] = $path;
- return $instance;
- }
- /**
- * 設(shè)置緩存文件前綴
- *
- * @param srting $prefix
- * @return self
- */
- public static function setCachePrefix($prefix)
- {
- $instance = self::getInstance();
- $instance->_options['file_name_prefix'] = $prefix;
- return $instance;
- }
- /**
- * 設(shè)置緩存存儲(chǔ)類型
- *
- * @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(emptyempty($glob))
- {
- return false;
- }
- foreach ($glob as $v)
- {
- $fileName = basename($v);
- $id = $instance->_fileNameToId($fileName);
- $instance->delete($id);
- }
- return true;
- }
- }
- /* 初始化設(shè)置cache的配置信息什么的 */
- cache::setCachePrefix('core'); //設(shè)置緩存文件前綴
- cache::setCacheDir('./cache'); //設(shè)置存放緩存文件夾路徑
- //模式1 緩存存儲(chǔ)方式
- //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 緩存存儲(chǔ)方式
- /*
- <?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;
- }
- /**
- * 設(shè)置參數(shù)
- * @param array $options 緩存參數(shù)
- * @return void
- */
- static function &setOptions($options = array())
- {
- return self::getInstance($options);
- }
- /**
- * 構(gòu)造函數(shù)
- * @param array $options 緩存參數(shù)
- * @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 不能為空 ');
- }
- }
- /**
- * 設(shè)置緩存路徑
- * @param string $value
- * @return void
- */
- static function setCacheDir($value)
- {
- $self = & self::getInstance();
- if (!is_dir($value)) {
- exit('file_cache: ' . $value.' 不是一個(gè)有效路徑 ');
- }
- if (!is_writable($value)) {
- exit('file_cache: 路徑 "'.$value.'" 不可寫');
- }
- $value = rtrim($this->_options['cache_dir'],'/') . '/';
- $self->_options['cache_dir'] = $value;
- }
- /**
- * 存入緩存數(shù)據(jù)
- * @param array $data 放入緩存的數(shù)據(jù)
- * @param string $id 緩存id(又名緩存識(shí)別碼)
- * @param cache_life 緩存時(shí)間
- * @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 = "<?phpn".
- " // mktime: ". $time. "n".
- " return ".
- var_export($data, true).
- "n?>"
- ;
- $res = $self->_filePutContents($file, $data);
- return $res;
- }
- /**
- * 得到緩存信息
- *
- * @param string $id 緩存id
- * @return string|array 緩存數(shù)據(jù)
- */
- static function load($id)
- {
- $self = & self::getInstance();
- $time = time();
- //檢測(cè)緩存是否存在
- if (!$self->test($id)) {
- // The cache is not hit !
- return false;
- }
- //全部清空識(shí)別文件
- $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(emptyempty($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 緩存內(nèi)容
- */
- 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);
- }
- }
- 存入數(shù)據(jù)
- <?php
- $config = array(
- 'name' => 'xiaojiong',
- 'qq' => '290747680',
- 'age' => '20',
- );
- //第一個(gè)參數(shù) 緩存data
- //第二個(gè)參數(shù) 緩存id
- //第三個(gè)參數(shù) cache_life 0 永不過(guò)期(cache::clear()清空所有除外) 默認(rèn)cache_life 為option_cache_life
- cache::save($config,'config',0);
- 載入數(shù)據(jù)
- <?php
- //只有一個(gè)參數(shù) cache_id
- $config = cache::load('config');
- 清空緩存
- <?php
- //清空指定緩存
- cache::del('config');
- //清空所有緩存
- cache::clear();
- cache信息配置
- //在執(zhí)行所有cache_func前調(diào)用
- $_options = array(
- 'cache_dir' => './cache', //緩存文件目錄
- 'file_name_prefix' => 'cache',//緩存文件前綴
- 'file_life' => 100000, //緩存文件生命
- );
- cache::setOptions($options);
- //再執(zhí)行 就會(huì)按著新配置信息執(zhí)行,否則是默認(rèn)信息
- cache::save($arr,'arr');
- //就是這個(gè)方法 貌似不合理 望大家指點(diǎn)
- ?>
新聞熱點(diǎn)
疑難解答