05 使用静态缓存

<?php 


class File{

    public  $_dir;

    const EXT='.txt';

    public function __construct(){

        $this->_dir=dirname(__FILE__).'/file/';
    }



    public function cacheData($key,$value='',$path=''){

         $filename=$this->_dir.$path.$key.self::EXT;

         #删除
        if(is_null($value)){

             return unlink($filename);
        }

   
        #创建
        if($value!=""){

             $dir=dirname($filename);

             if(!is_dir($dir)){

                mkdir($dir,0777);
             }

            return  file_put_contents($filename, json_encode($value));
        }
         
        #获取
         if(!is_file($filename)){

             return false;

         }else{
             return json_decode(file_get_contents($filename),true);
         }



    }
}

   $data=array(
    'id'=>1,
    'name'=>'hgj123',
   );

  $file=new File();
  //var_dump($file->cacheData('cachae'));
  if($file->cacheData('cachae',null)){
      echo "OK";

  }else{
    echo "NO";
  }
原文地址:https://www.cnblogs.com/hgj123/p/4354155.html