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
- php缓存查询请求
- PHP 缓存请求
- 如何让php对页面的缓存进行控制 php处理请求的浏览器缓存处理
- php缓存
- PHP缓存
- PHP 缓存
- php缓存
- php缓存
- ajax请求缓存问题
- ajax请求缓存问题
- AJAX请求 清除缓存
- HTTP请求、响应、缓存
- HTTP请求、响应、缓存
- 浏览器缓存url请求
- HTTP 请求、响应、缓存
- iOS 请求图片缓存
- 浏览器缓存url请求
- 浏览器缓存url请求
- linux进程、线程状态 tomcat线程数 并发数查看
- 字符驱动函数-mmap分析
- java程序连接数据库小练手
- 考试篇(5.2) NSE4 题库 13. 透明模式 ❀ 飞塔 (Fortinet) 网络安全专家
- supervisor(三)xml_rpc
- PHP 缓存请求
- centos使用CP命令不提示覆盖
- 大型网站架构系列:20本技术书籍推荐
- cocos2d-x lua 使用自定义消息EventCustom
- Web.xml
- Discuz! 3.2 中各数据库表的作用
- 使用代码获得项目的SHA1
- dubbo拓展点机制的IOC特性
- 尺度不变特征变换匹配算法详解