PHP Development APP Interface Summary-static cache

Source: Internet
Author: User
Tags filetime delete cache

Storing a static cache writes the cache to a file.

file.php

<?PHPclasscache{//static cache file suffix name    ConstEXT = ' txt '; //defining cache file Storage Paths    Private $_dir;  Public function__construct () {$this->_dir =dirname(__file__).‘ /files/'; }     Public functionCacheData ($k,$v= ",$path= ' '){        //file name        $filename=$this->_dir.$path.$k.‘.‘. Self::EXT; //$v not for ': Store cache or delete cache        if($v!== "){            //Delete Cache            if(Is_null($v)){                return@unlink($filename); }            //Storage Cache            $dir=dirname($filename); if(!Is_dir($dir)){                mkdir($dir, 0777); }            //Turn the $v into a string type            return file_put_contents($filename, Json_encode ($v)); }        //Read Cache        if(!Is_file($filename)){            return false; }Else{            returnJson_decode (file_get_contents($filename),true); }    }}

testfile.php

<?PHPrequire' File.php ';$data=Array(    ' id ' =>1, ' name ' = ' Mary ', ' type ' and ' = 'Array(1,3,6));$file _cache=NewCache ();//Storage Cacheif($file _cache->cachedata (' Index_cache ',$data)){    Echo' Success ';}Else{    Echo' Error ';}//Read Cacheif($con=$file _cache->cachedata (' Index_cache ')){    Var_dump($con);}Else{    Echo' Error ';}//Delete Cacheif($con=$file _cache->cachedata (' Index_cache ',NULL)){    Echo' Delete Success ';}Else{    Echo' Error ';}

======

Modify it slightly, set the cache for n minutes, regenerate the cache for more than n minutes, or read the data from the cache.

In file.php, file name and file modification time are also stored in cached data when storing data.

<?PHPclasscache{//static cache file suffix name    ConstEXT = ' txt '; //defining cache file Storage Paths    Private $_dir;  Public function__construct () {$this->_dir =dirname(__file__).‘ /files/'; }     Public functionCacheData ($k,$v= ",$path= ' '){        //file name        $filename=$this->_dir.$path.$k.‘.‘. Self::EXT; //$v not for ': Store cache or delete cache        if($v!== "){            //Delete Cache            if(Is_null($v)){                return@unlink($filename); }            //Storage Cache            $dir=dirname($filename); if(!Is_dir($dir)){                mkdir($dir, 0777); }            //Turn the $v into a string type            $_return=Array(                ' filename ' =$filename, ' filetime ' = @Filemtime($filename),//file Creation (modification) time' Con ' = Json_encode ($v)            ); return file_put_contents($filename, Json_encode ($_return)); }        //Read Cache        if(!Is_file($filename)){            return false; }Else{            returnJson_decode (file_get_contents($filename),true); }    }}

testfile.php

<?PHPrequire' File.php ';$data=Array(    ' id ' =>1, ' name ' = ' Mary ', ' type ' and ' = 'Array(1,3,6));$file _cache=NewCache ();//set the 5min cache, regenerate the cache more than 30s, or read the data from the cache$k= ' Index_cache ';$countdown= 5*60;$con=$file _cache->cachedata ($k);if($con){//If you can read the cache    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{//Create a cache if the cache does not exist    $file _cache->cachedata ($k,$data); Var_dump($data);}

PHP Development APP Interface Summary-static cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.