PHP 缓存请求 改~
想找一下PHP缓存请求方面的内容,然后 bing 一下,找到一个........
哇塞,发现是自己的文章,被偷偷转发了.................
自己有过的设计都忘记了.........
好吧,就看看自己过去的代码............
缓存那里好像少了点东西,只能缓存一个请求,忘记了追加请求的文件做标识,修正。
然后发现,缓存了全部请求参数做标识,追加了一个 only 函数来,指定哪些参数可以做标识。
test.php的源码:
MZCRequestCache.class.php 源码:
<?php
class MZCRequestCache {
var $dirName;
var $isDirHere;
var $requestAPI;
var $requestBodyMD5;
function __construct() {
$this->requestAPI = '';
$this->requestAPI = $_SERVER["PHP_SELF"];
$this->requestAPI = str_replace('/', '__', $this->requestAPI);
$this->requestAPI = str_replace('.', '_', $this->requestAPI);
// echo("API:[" . $this->requestAPI."]<br>");
$this->requestBody = '';
if ($_POST) {
$this->requestBody = file_get_contents('php://input', 'r');
} else {
$this->requestBody = $_SERVER["QUERY_STRING"];
}
// echo("请求体:[" . $this->requestBody."]<br>");
$this->requestBodyMD5 = '';
$targetString = $this->requestAPI . '?'. $this->requestBody;
$this->requestBodyMD5 = md5($targetString);
$this->dirName = 'requestCache';
$this->isDirHere = FALSE;
if (is_dir($this->dirName)) {
//echo '<br/>requestCache dir here!';
$this->isDirHere = TRUE;
} else {
//echo '<br/>requestCache dir no here!';
if (mkdir($this->dirName)) {
//echo '<br/>requestCache create scuess!';
$this->isDirHere = TRUE;
} else {
//echo '<br/>requestCache create fail!!';
$this->isDirHere = FALSE;
}
}
}
function only($arr = []) {
if (is_array($arr) == FALSE) {
return;
}
$keepArray = array();
foreach($arr as $x=>$x_value) {
$requestBodyArray = explode('&',$this->requestBody);
foreach($requestBodyArray as $y=>$y_value) {
$tempArray = explode('=',$y_value);
if (is_array($tempArray)) {
if (strcmp($x_value, $tempArray[0]) == 0) {
$keepArray[$tempArray[0]] = $tempArray[1];
}
}
}
}
$keepArray2 = array();
$index = 0;
foreach($keepArray as $as=>$as_value) {
$keepArray2[$index] = $as.'='.$as_value;
$index++;
}
$targetString = implode('&',$keepArray2);
$targetString2 = $this->requestAPI . '?'. $targetString;
$this->requestBodyMD5 = '';
$this->requestBodyMD5 = md5($targetString2);
// echo('$targetString2 > ['.$targetString2.']<br>');
}
/*获取缓存*/
function getCache() {
if ($this->isDirHere) {
$cacheDir = $this->dirName.'/'.$this->requestAPI;
$isCacheDirHere = FALSE;
if (is_dir($cacheDir)) {
$isCacheDirHere = TRUE;
}
if ($isCacheDirHere) {
$path = $this->dirName.'/'.$this->requestAPI.'/'.$this->requestBodyMD5;
$content = file_get_contents($path);
return $content;
}
}
}
/*判断是否有缓存*/
function haveCache() {
//echo '<br/>$isDirHere-->'.$this->isDirHere;
if ($this->isDirHere) {
//echo '<br/>$requestBodyMD5-->'.$this->requestBodyMD5;
$path = $this->dirName.'/'.$this->requestAPI.'/'.$this->requestBodyMD5;
//echo '<br/>$path--->'.$path;
if (file_exists($path)) {
return TRUE;
}
}
return FALSE;
}
/*清除缓存*/
function removeCache() {
if ($this->isDirHere) {
$cacheDir = $this->dirName.'/'.$this->requestAPI;
return $this->deldir($cacheDir);
}
return FALSE;
}
/*清除指定的缓存*/
function removeCache1($requestAPI) {
if ($this->isDirHere) {
$cacheDir = $this->dirName.'/'.$requestAPI;
return $this->deldir($cacheDir);
}
return FALSE;
}
/*创建缓存*/
function createCache($content) {
if ($this->isDirHere) {
$cacheDir = $this->dirName.'/'.$this->requestAPI;
$isCacheDirHere = FALSE;
if (is_dir($cacheDir)) {
$isCacheDirHere = TRUE;
} else {
if (mkdir($cacheDir)) {
$isCacheDirHere = TRUE;
} else {
$isCacheDirHere = FALSE;
}
}
if ($isCacheDirHere) {
$path = $this->dirName.'/'.$this->requestAPI.'/'.$this->requestBodyMD5;
$fh = fopen($path, "a");
fwrite($fh, $content);
fclose($fh);
}
}
}
function deldir($dir) {
//先删除目录下的文件:
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if (rmdir($dir)) {
return true;
} else {
return false;
}
}
//选择数据库
//实现类的函数的重载
function __call($name, $args) {
$isOK = false;
if ($name == 'removeCache') {
$isOK = true;
}
if ($isOK) {
$i = count($args);
if (method_exists($this, $f = $name . $i)) {
call_user_func_array(array($this, $f), $args);
}
}
}
}
?>