PHP緩存類

ouhp9343 8年前發布 | 975 次閱讀 PHP


使用 

<?php
header("content-type:text/html;charset:utf-8");

require_once 'cache.class.php';

$dir = dirname(__FILE__).'/cache_data';

$cache = new Cache($dir);

$cache->add(1, 'test');

$cache->set(2, 'abc',60);

$cache->get(1); 

if(!$cache->get(1)){
    echo $cache->tip_message;
}else{
    echo $cache->get(1);
}

$cache->delete(1);

$cache->flush();

$cache->auto_delete_expired_file();

cache.class.php&nbsp;~&nbsp;7KB&nbsp;&nbsp;&nbsp;&nbsp;下載(61)

<?php
// +----------------------------------------------------------------------
// |緩存類
// +----------------------------------------------------------------------
// | Author: justmepzy(justmepzy@gmail.com)
// +----------------------------------------------------------------------

class Cache{

    //提示信息
    public $tip_message;

    //緩存目錄
    protected $cache_dir;
    //緩存文件名
    private $cache_file_name;
    //緩存文件后綴
    private $cache_file_suffix;

    public function __construct($dir,$cache_file_suffix = '.php'){
        $this->cache_dir = isset($dir)?$dir:dirname(__FILE__).DIRECTORY_SEPARATOR.'default_cache_data';

        $this->cache_file_suffix = $cache_file_suffix;

        if(!$this->dir_isvalid($this->cache_dir)){
            die($this->tip_message);//創建目錄失敗
        }

    }
    // +----------------------------------------------------------------------
    // |添加一個值,如果已經存在,則返回false,寫入文件失敗返回false
    // +----------------------------------------------------------------------
    // | 
    // +----------------------------------------------------------------------
    public function add($cache_key,$cache_value,$life_time=1800){
         if(file_exists($this->get_cache_file_name($cache_key))){
            $this->tip_message = '緩存數據已存在.';
            return false;
         }
         $cache_data['data'] = $cache_value;
         $cache_data['life_time'] = $life_time;
         //以JSON格式寫入文件
         if(file_put_contents($this->get_cache_file_name($cache_key), json_encode($cache_data))){
            return true;
         }else{
            $this->tip_message = '寫入緩存失敗.';
            return false;
         }

    }
    // +----------------------------------------------------------------------
    // |添加一個值,如果已經存在,則覆寫
    // +----------------------------------------------------------------------
    // | 
    // +----------------------------------------------------------------------
    public function set($cache_key,$cache_value,$life_time=1800){

        $cache_data['data'] = $cache_value;
        $cache_data['life_time'] = $life_time;
        if(file_put_contents($this->get_cache_file_name($cache_key), json_encode($cache_data))){
            return true;
         }else{
            $this->tip_message = '寫入緩存失敗.';
            return false;
         }
    }
    // +----------------------------------------------------------------------
    // |獲取一個key值
    // +----------------------------------------------------------------------
    // | 
    // +----------------------------------------------------------------------
    public function get($cache_key){
        if(!file_exists($this->get_cache_file_name($cache_key))){
            return false;
        }
        $data = $this->object_to_array(json_decode(file_get_contents($this->get_cache_file_name($cache_key))));

        if($this->check_isvalid($data['life_time'])){
            unset($data['life_time']);
            return $data['data'];
        }else{
            unlink($this->cache_file_name);
            $this->tip_message = '數據已過期.';
            return false;
        }   
    }
    // +----------------------------------------------------------------------
    // |刪除一個key值
    // +----------------------------------------------------------------------
    // | 
    // +----------------------------------------------------------------------
    public function delete($cache_key){
        if(file_exists($this->get_cache_file_name($cache_key))){
            if(unlink($this->get_cache_file_name($cache_key)))
                return true;
            else
                return false;
        }else{
            $this->tip_message = '文件不存在.';
            return true;
        }
    }
    // +----------------------------------------------------------------------
    // |清除所有緩存文件
    // +----------------------------------------------------------------------
    // | 
    // +----------------------------------------------------------------------
    public function flush(){
        $this->delete_file($this->cache_dir);
    }
    // +----------------------------------------------------------------------
    // |自動清除過期文件
    // +----------------------------------------------------------------------
    // | 
    // +----------------------------------------------------------------------
    public function auto_delete_expired_file(){
        $this->delete_file($this->cache_dir,false);
    }
    // +----------------------------------------------------------------------
    // |檢查目錄是否存在,不存在則創建
    // +----------------------------------------------------------------------
    // | 
    // +----------------------------------------------------------------------
    private function dir_isvalid($dir){

        if (is_dir($dir)) 
            return true;
        try {
           mkdir($dir,0777);
        }catch (Exception $e) {
             $this->tip_message = '所設定緩存目錄不存在并且創建失敗!請檢查目錄權限!';
             return false;            
       }
       return true;
    }
    // +----------------------------------------------------------------------
    // |檢查有效時間
    // +----------------------------------------------------------------------
    // | 
    // +----------------------------------------------------------------------
    private function check_isvalid($expired_time = 0) {
        //if(!file_exists($this->cache_file_name)) return false;
        if (!(@$mtime = filemtime($this->cache_file_name))) return false;
        if (time() -$mtime > $expired_time) return false;
        return true;
   }
    // +----------------------------------------------------------------------
    // |獲得緩存文件名
    // +----------------------------------------------------------------------
    // | 
    // +----------------------------------------------------------------------
    private function get_cache_file_name($key){
        $this->cache_file_name = $this->cache_dir.DIRECTORY_SEPARATOR.md5($key).$this->cache_file_suffix;
        return $this->cache_file_name;
    }
    // +----------------------------------------------------------------------
    // |object對象轉換為數組
    // +----------------------------------------------------------------------
    // | 
    // +----------------------------------------------------------------------
    protected function object_to_array($obj){

        $_arr = is_object($obj) ? get_object_vars($obj) : $obj; 
        foreach ($_arr as $key => $val) { 
            $val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val; 
            $arr[$key] = $val; 
        } 
        return $arr; 
    }
    // +----------------------------------------------------------------------
    // |刪除目錄下的所有文件
    // +----------------------------------------------------------------------
    // | $mode true刪除所有 false刪除過期
    // +----------------------------------------------------------------------
    protected function delete_file($dir,$mode=true) {  
        $dh=opendir($dir); 
        while ($file=readdir($dh)) { 
            if($file!="." && $file!="..") { 
                $fullpath=$dir."/".$file; 
                if(!is_dir($fullpath)) { 
                    if($mode){
                        unlink($fullpath); 
                    }else{
                        $this->cache_file_name = $fullpath;
                        if(!$this->get_isvalid_by_path($fullpath)) unlink($fullpath); 
                    }

                } else { 
                    delete_file($fullpath,$mode); 
                } 
            } 
        } 
        closedir($dh); 
    }
    private function get_isvalid_by_path($path){
        $data = $this->object_to_array(json_decode(file_get_contents($path)));
        return $this->check_isvalid($data['life_time']);
    }
}
?>
 本文由用戶 ouhp9343 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!