(1) 静态 缓存 这里技术的本质是我们将我们所需要的一些数据临时存在服务器的一些文件中。 ?php class File { private $_dir ; const EXT= 'php' ; public function __construct () { $this -_dir=dirname( __FILE__ ). '/files/' ; } public function cache
(1)静态缓存
这里技术的本质是我们将我们所需要的一些数据临时存在服务器的一些文件中。
class File
{
private $_dir;
const EXT='php';
public function __construct()
{
$this->_dir=dirname(__FILE__).'/files/';
}
public function cacheDate($key,$value='',$path='')
{
$filename=$this->_dir.$path.$key.self::EXT;
//将value值写入缓存
if($value!=='')
{
//如果value值为null
if(is_null($value))
{
return unlink($filename);
}
$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);
}
}
}
这里封装了一个类,下面是测试代码
Difeye是一款超轻量级PHP框架,主要特点有: Difeye是一款超轻量级PHP框架,主要特点有: ◆数据库连接做自动主从读写分离配置,适合单机和分布式站点部署; ◆支持Smarty模板机制,可灵活配置第三方缓存组件; ◆完全分离页面和动作,仿C#页面加载自动执行Page_Load入口函数; ◆支持mysql,mongodb等第三方数据库模块,支持读写分离,分布式部署; ◆增加后台管理开发示例
require_once './File.php';
$data=array(
'id'=>1,
'name'=>'singwa',
'type'=>array(4,5,6),
'test'=>array(1,45,67=>array(123,'tsysa')),
);
$file=new File();
//增加缓存的方式 这样会将指定的数据放入指定文件夹中
if($file->cacheDate('index_mk_cache'),$data)
{
var_dump($file->cacheDate('index_mk_cache'));
echo "success";
}
else
{
echo "error";
}
//取出缓存的方式
if($file->cacheDate('index_mk_cache'))
{
var_dump($file->cacheDate('index_mk_cache'));
echo "success";
}
else
{
echo "error";
}
//删除缓存的方式
if($file->cacheDate('index_mk_cache'),$value=null)
{
var_dump($file->cacheDate('index_mk_cache'));
echo "success";
}
else
{
echo "error";
}










