//测试
$cache = new Cache();
$cache->dir = "../cc/";
//$cache->setCache("zhang", "zhangsan", 100);
echo $cache->getCache("zhang");
//$cache->removeCache("zhang");
$cache->setCache("liu", "liuqi", 100);
echo $cache->getCache("liu");
class Cache{
public $cacheFile = "cache.json"; //文件
public $dir = "./cach2/"; //目录
//缓存
public function setCache($name, $val, $expires_time){
$file = $this->hasFile();
//字符串转数组
$str = file_get_contents($file);
$arr = json_decode($str, true);
//值为空,则移除该缓存
if(empty($val)){
unset($arr[$name]);
}else{
$arr[$name] = array("value"=>$val, "expires_time"=>$expires_time, "add_time"=>time());
}
//数组转字符串
$str = json_encode($arr);
file_put_contents($file, $str);
}
public function getCache($name){
$file = $this->hasFile();
//字符串转数组
$allArr = json_decode($str, true);
$arr = $allArr[$name];
if(!$arr || time() > ($arr["expires_time"] + $arr["add_time"])){
$this->removeCache($name); //过期移除
return false;
}
return $arr["value"];
}
public function removeCache($name){
$this->setCache($name, '', 0);
}
private function hasFile(){
//如果不存在缓存文件,则创建一个
if(!file_exists($this->dir)){
mkdir($this->dir);
}
if(!file_exists($this->dir . $this->cacheFile)){
touch($this->dir . $this->cacheFile);
}
return $this->dir . $this->cacheFile;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有