静态缓存
保存在磁盘上的静态文件,用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';
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号