码迷,mamicode.com
首页 > Web开发 > 详细

PHP 写入缓存

时间:2017-07-14 16:45:18      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:target   文件   white   put   echo   false   .text   常量   判断目录   

1.创建file.PHP

<?php
class File{
//封装方法
private $_dir;
const EXT=‘.text‘;//文件后缀,定义为常量


public function __contruct(){
$this->_dir=dirname(__FILE__).‘/files/‘;//默认存货缓存文件夹数据
}

//$key文件名,$path,路径
public function cacheData($key,$value="",$path=""){
$filename=$this->_dir.$path.$key.self::EXT;
//有值,写入缓存
if($value !==""){
$dir=dirname($filename);//判断目录是否存在
if(!is_dir($dir)){
mkdir($dir,0777);
}
return file_put_contents($filename,json_encode($value));//写入成功,返回数据,失败false

}
}
}

?>


2.创建text.php

<?php
require_once(‘./file.php‘);
$data=array(
‘id‘=>1,
‘name‘=>‘niao‘,
‘type‘=>array(4,5,6),
‘test‘=>array(1,45,67)
);

$file=new File();
if($file->cacheData(‘index_mk_cache‘,$data)){
echo ‘succ‘;
}else{
echo ‘error‘;

}

?>


注意:生成一个index_mk_cache.txt格式的文件.里面包含写入的数据


PHP 写入缓存

标签:target   文件   white   put   echo   false   .text   常量   判断目录   

原文地址:http://www.cnblogs.com/lqylqy/p/7170558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!