php app接口开发-2
创建fiel.php
<?php class File { private $_dir; const EXT = ‘.txt‘; public function __construct() { $this->_dir = dirname(__FILE__) . ‘/files/‘; } public function cacheData($key, $value = ‘‘, $cacheTime = 0) { $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); } $cacheTime = sprintf(‘%011d‘, $cacheTime); return file_put_contents($filename,$cacheTime . json_encode($value)); } if(!is_file($filename)) { return FALSE; } $contents = file_get_contents($filename); $cacheTime = (int)substr($contents, 0 ,11); $value = substr($contents, 11); if($cacheTime !=0 && ($cacheTime + filemtime($filename) < time())) { unlink($filename); return FALSE; } return json_decode($value, true); } } $file = new File(); echo $file->cacheData(‘test1‘);
通过test.php调用file.php,访问app_api.com/test.php
<?php require_once("./file.php"); $data = array( "name" => "bk", "age" => 25 ); $file = new File(); if($file->cacheData("index_mk_cache",$data)){ echo "success"; }else{ echo "error"; }
相关推荐
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23