为什么获取缓存不成功呢
获取缓存不成功呢
获取缓存不成功呢
2015-03-30
<?php
//调用文件操作类
// include "./resu.class.php";
//error_reporting(0);
include "file.php";
$data= array(
'id'=>1,
'name'=>'singwa',
'type'=>array(4,5,6),
'test'=>array(1,45,67=>array(1,'45698'),),
);
// resu::show(200,'success',$data,'json');
$file = new File();
if($file->cacheData('index_mk_cache')){
var_dump($file->cacheData('index_mk_cache'));exit();
echo "seccess";
}else{
echo 'error';
}
<?php
class File{
private $_dir;
const EXT = '.txt';
public function __construct() {
$this->_dir = dirname(__FILE__) . '/files/';
}
public function cacheData($key,$value,$path=''){
$filename = $this->_dir . $key . self::EXT;
if($value != '') { // 将value值写入缓存
/*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);
}
}
}
举报