国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 泉州市| 闸北区| 慈溪市| 从江县| 清原| 新民市| 绿春县| 高要市| 鄂托克前旗| 西乡县| 宁海县| 原阳县| 云南省| 大同县| 故城县| 金沙县| 安远县| 龙泉市| 卢龙县| 白玉县| 剑阁县| 舞钢市| 城市| 江永县| 梅河口市| 定结县| 基隆市| 建瓯市| 平南县| 岫岩| 同心县| 凤山市| 湟中县| 麻栗坡县| 广宗县| 慈利县| 得荣县| 尼玛县| 达尔| 大渡口区| 柯坪县|