Blogger Information
Blog 40
fans 0
comment 0
visits 45672
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
缓存
无須終有的博客
Original
605 people have browsed it
  1. 静态缓存

    保存在磁盘上的静态文件,用php生成数据放入静态文件中

        >1生成缓存

        >2获取缓存

        >3删除缓存

2.创建一个包含添加,删除,读取静态缓存的方法

file.php

<?php  
header('content-type:text/html;charset=utf-8');
class File
{
    private $_dir;
    const EXT='.txt';
    public function __construct()
    {
        // echo dirname(__FILE__);die;
        $this->_dir=dirname(__FILE__).'/files/';
    }
    /*
     @param string $key 文件名
     @param array  $value 传输的数据
     @param string $path 路径
     */
    public function cacheData($key,$value='',$path='')
    {
        $filename=$this->_dir.$path.$key.self::EXT;//E:\WWW\malt/files/index_mk_cache.txt
        if($value!==''){//将value值写入缓存
            if(is_null($value)){
            return @unlink($filename);
            }
        $dir=dirname($filename);//E:\WWW\malt/files
        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);
    }
    }
}

test.php

<?php  
require_once('./file.php');
$data=array(
    'id'=>1,
    'name'=>'singwa',
    'type'=>'array(4,5,6)',
    'test'=>array(1,34,56=>array(213,'tsysa')),
);
$file=new File();
//第二个参数为null的时候删除
if($file->cacheData('index_mk_cache',$data)){
    // var_dump($file->cacheData('index_mk_cache'));exit;
    echo 'success';
}else{
    echo 'error';
}



Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post