PHP 缓存请求

前提:


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



思路:


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


完成:



UML:



代码:

<?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);
		
		$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.....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值