月小升研究完毕静态缓存,立即改了一套redis,这内存数据库的速度就是惊人
在java-er.com wordpress根目录建立文件cacheredis.php内容如下
<?php $iscache = 1;//决定不缓存 $key = md5($_SERVER['REQUEST_URI']); //缓存key if($_GET['preview']=='true')$iscache = 0; //预览的文章不缓存 if($_SERVER['REQUEST_METHOD']=='GET' && $iscache==1){ //GET方式请求才缓存,POST之后一般都希望看到最新的结果 $redis = new Redis(); $redis_state = 1; try{ $redis->connect('127.0.0.1', 6379); }catch(Exception $e){ $redis_state=0; //如果链接不上,说明挂了 } if($redis_state==1){ //如果redis是好的,我就接着跑 if($redis->exists($key)){ $contents = $redis->get($key); echo $contents." <!-- redis cache java-er.com -->"; exit(); }else{ function auto_cache($contents){ //回调函数,当程序结束时自动调用此函数 global $redis,$key; $redis->set($key,$contents); return $contents; } ob_start('auto_cache'); //回调函数 auto_cache } } } ?>
首页index.php头部进行调用
require('cacheredis.php');