1. 程式人生 > >PHP 開發 APP 介面--靜態快取篇

PHP 開發 APP 介面--靜態快取篇

儲存靜態快取即把快取寫入檔案。

 /**
*按綜合方式輸出通訊資料
 *@param string $k 檔名
 *@param string $v 快取資料
 *@param string $path 路徑
 *@return string
 */

file.php

複製程式碼
<?php
class Cache{
    //靜態快取檔案字尾名
    const EXT = 'txt';
    //定義快取檔案存放路徑
    private $_dir;
    public function __construct(){
        $this->_dir = dirname(__FILE__).'/files/';
    }

    public function cacheData($k,$v = '',$path = ''){
        //檔名
        $filename = $this->_dir.$path.$k.'.'.self::EXT;
        //$v不為‘’:儲存快取或者刪除快取
        if($v !== ''){

            //刪除快取
            if(is_null($v)){
                return @unlink($filename);
            }

            //儲存快取
            $dir = dirname($filename);
            if(!is_dir($dir)){
                mkdir($dir,0777);
            }
            //把$v轉成string型別
            return file_put_contents($filename,json_encode($v));
        }

        //讀取快取
        if(!is_file($filename)){
            return false;
        }else{
            return json_decode(file_get_contents($filename),true);
        }
    }
}
複製程式碼

testfile.php

複製程式碼
<?php
require 'file.php';

$data = array(
    'id'=>1,
    'name'=>'Mary',
    'type'=>array(1,3,6)
);

$file_cache = new Cache();
//儲存快取
if($file_cache->cacheData('index_cache',$data)){
    echo 'success';
}else{
    echo 'error';
}

//讀取快取
if($con = $file_cache->cacheData('index_cache')){
    var_dump($con);
}else{
    echo 'error';
}

//刪除快取
if($con = $file_cache->cacheData('index_cache',null)){
    echo 'delete success';
}else{
    echo 'error';
}
複製程式碼

 ======

稍微修改一下,設定n分鐘的快取,超過n分鐘則重新生成快取,否則從快取中讀取資料。

在file.php 中,儲存資料時把檔名和檔案修改時間也同時存入快取資料

複製程式碼
<?php
class Cache{
    //靜態快取檔案字尾名
    const EXT = 'txt';
    //定義快取檔案存放路徑
    private $_dir;
    public function __construct(){
        $this->_dir = dirname(__FILE__).'/files/';
    }

    public function cacheData($k,$v = '',$path = ''){
        //檔名
        $filename = $this->_dir.$path.$k.'.'.self::EXT;
        //$v不為‘’:儲存快取或者刪除快取
        if($v !== ''){

            //刪除快取
            if(is_null($v)){
                return @unlink($filename);
            }

            //儲存快取
            $dir = dirname($filename);
            if(!is_dir($dir)){
                mkdir($dir,0777);
            }
            //把$v轉成string型別
            $_return = array(
                'filename' => $filename,
                'filetime' => @filemtime($filename), //檔案建立(修改)時間
                'con' => json_encode($v)
            );
            return file_put_contents($filename,json_encode($_return));
        }

        //讀取快取
        if(!is_file($filename)){
            return false;
        }else{
            return json_decode(file_get_contents($filename),true);
        }
    }
}
複製程式碼

testfile.php

複製程式碼
<?php
require 'file.php';

$data = array(
    'id'=>1,
    'name'=>'Mary',
    'type'=>array(1,3,6)
);

$file_cache = new Cache();

//設定5min的快取,超過30s則重新生成快取,否則從快取中讀取資料
$k = 'index_cache';
$countdown = 5*60;
$con = $file_cache->cacheData($k);

if($con){ //如果能夠讀取快取
    if(time()-$con['filetime'] > 30){
        $file_cache->cacheData($k,$data);
        var_dump($data);
    }else{
        $res = $file_cache->cacheData($k);
        if($res){
            var_dump(json_decode($res['con'],true));
        }    
    }
}else{    //如果快取不存在則建立快取
    $file_cache->cacheData($k,$data);
    var_dump($data);
}
複製程式碼