国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP文件緩存類示例分享

2024-05-04 23:30:49
字體:
供稿:網(wǎng)友
這里給大家分享一個php文件緩存類,十分實用,給需要的小伙伴們參考下。
 

 

復(fù)制代碼代碼如下:

<?php
    /**
     * @desc 文件緩存
     */
    class Cache{
        const C_FILE = '/Runtime/';
        private $dir = '';
        const EXT = '.tpl';
        private $filename = '';
        public function __construct($dir = ''){
            $this->dir = $dir;
        }
        /**
         *    @desc 設(shè)置文件緩存
         *    @param string $key 文件名 
         *    @param unkonw $data 緩存數(shù)據(jù)
         *    @param int    $expire 過期時間
         */
        public function set($key,$data,$expire = 0){
            $this->filename =     dirname(__FILE__).self::C_FILE.$this->dir.$key.self::EXT;
            if(file_exists($this->filename)){
                $res = $this->get($key);
                if(md5($res) == md5(json_encode($data) ) ){
                    return true;
                }
            }
            if(!is_dir(dirname($this->filename))){
                mkdir(dirname($this->filename),0777);
            }
            $source = fopen($this->filename,'w+');
            fwrite($source,json_encode($data));
            fclose($source);
        }
        /**
         *    @desc 獲取文件
         *  @param string $key 文件名
         */
        public function get($key){
            //$filename = dirname(__FILE__).self::C_FILE.$this->dir.$key.self::EXT;
            if(!file_exists($this->filename)){
                return '緩存文件已經(jīng)不存在';
            }else{
                $res = file_get_contents($this->filename);
            }
            return $res;
        }
        /**
         *    @desc 刪除文件
         *  @param string $key 文件名
         */
        public function del($key){
            unlink($this->filename);
        }
    }
    $data = array('name'=>'song','age'=>20,'sex'=>'man','favority'=>array('apple','banana'));
    $cache = new Cache();
    $cache->set('cache',$data);
    //$cache->get('cache');
    //$cache->del('cache');

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 建湖县| 乐陵市| 滦平县| 东平县| 交口县| 成武县| 南丹县| 都匀市| 二手房| 会宁县| 黄平县| 盖州市| 蒲江县| 元江| 平湖市| 越西县| 寿阳县| 福鼎市| 荥经县| 托克逊县| 饶平县| 峨眉山市| 保康县| 蒙阴县| 互助| 金川县| 青冈县| 军事| 丽江市| 新蔡县| 九台市| 日喀则市| 横峰县| 马公市| 云梦县| 衡阳县| 施甸县| 中山市| 柳州市| 永和县| 武威市|