PHP 缓存请求

来源:互联网 发布:http是什么 java题 编辑:程序博客网 时间:2024/05/19 01:09

前提:


在公司遇到这样的问题:请求数据很慢,根据我了解是因为请求的数据关联很多的地方,服务器需要消耗相当资源来处理,于是我之前便思考出这个方案,现在终于有时间来实现。



思路:


把请求的结果缓存到服务器上,再次进行相同的请求时,就会调取缓存,减轻服务器的压力,提高请求数据的速度。


完成:



UML:



代码:

<?phpclass 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);$this->requestBody = '';if ($_POST) {$this->requestBody = file_get_contents('php://input', 'r'); } else {$this->requestBody = $_SERVER["QUERY_STRING"];}$this->requestBodyMD5 = '';$this->requestBodyMD5 = md5($this->requestBody);$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 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);}}}}?>


后续:

写完给各位大大群友看完之后,提出很多宝贵意见:什么加锁、解锁、什么读写磁盘开销大.....


(希望以后我会懂的......)

虽然这些我都不懂,但是根据我觉得我这些处理的话,至少可以当公司减轻负担,因为公司的后台可不是像各位大大那么厉害。

再说,其实我不是PHPer,我只是一个略懂PHP语法的打杂,平时做的最多就是iOS,但是iOS 不好混啊,希望可以改混到Unity.....

0 0
原创粉丝点击