发表于 2018-09-08 07:46:58 by 月小升
月小升研究完毕静态缓存,立即改了一套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
}
}
}
?> |
<?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'); |
require('cacheredis.php');
This entry was posted in
Linux,
PHP and tagged
cache,
redis,
缓存. Bookmark the
permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:
月小升博客 –
https://java-er.com/blog/redis-wordpress-cache/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.