加速WordPress技巧:Redis缓存输出的HTML页面

加速WordPress技巧:Redis缓存输出的HTML页面,第1张

加速WordPress技巧:Redis缓存输出的HTML页面

Redis是一个高级的键值存储系统,类似于memcached。所有内容都存储在运行内存中,因此每秒可以执行超过100,000次实际GET *** 作。

Redis是一个高級的key-value存储系统,相近memcached,全部内容都存有运行内存中,因而每秒能够超出10万次GET实际 *** 作。
我下面明确提出的解决方法是在Redis中缓存文件全部輸出的HTML内容而不用再让WordPress反复实行网页页面脚本制作。这儿应用Redis替代Varnish设定简易,并且很有可能更快。
安裝Redis
假如你应用的是Debian或是衍化的电脑 *** 作系统可应用以下指令安裝Redis:
apt-getinstallredis-server
或是阅读文章安裝手册
应用Predis做为Redis的PHP手机客户端
你需要一个移动端开发包便于PHP能够联接到Redis服务项目上。
这儿大家强烈推荐Predis.提交predis.php到WordPress的根目录。
前端开发缓存文件的PHP脚本制作
流程1:在WordPress的根目录建立新文档index-with-redis.php,内容以下:

复制代码编码以下:
<?php
//Changethesetwovariables:
$seconds_of_caching=60*60*24*7;//7days.
$ip_of_this_website='204.62.14.112';
/*
-ThisfileiswrittenbyJimWestergren,copyrightallrightsreserved.
-Seemorehere:www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
-Thecodeisfreeforeveryonetousehowtheywantbutpleasementionmynameandlinktomyarticlewhenwritingaboutthis.
-Change$ip_of_this_websitetotheIPofyourwebsiteabove.
-Add?refresh=yestotheendofaURLtorefreshit'scache
-Youcanalsoentertheredisclientviathecommandpromptwiththecommand"redis-cli"andthenremoveallcachewiththecommand"flushdb".
*/
//VerynecessaryifyouuseCloudfare:
if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])){
$_SERVER['REMOTE_ADDR']=$_SERVER['HTTP_CF_CONNECTING_IP'];
}
//ThisisfromWordPress:
define('WP_USE_THEMES',true);
//Startthetimer:
functiongetmicrotime($t){
list($usec,$sec)=explode("",$t);
return((float)$usec(float)$sec);
}
$start=microtime();
//InitiateredisandthePHPclientforredis:
include("predis.php");
$redis=newPredis\Client('');
//fewvariables:
$current_page_url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$current_page_url=str_replace('?refresh=yes','',$current_page_url);
$redis_key=md5($current_page_url);
//Thisfirstcaseiseithermanualrefreshcachebyadding?refresh=yesaftertheURLorsomebodypostingacomment
if(isset($_GET['refresh'])||substr($_SERVER['REQUEST_URI'],-12)=='?refresh=yes'||($_SERVER['HTTP_REFERER']==$current_page_url&&$_SERVER['REQUEST_URI']!='/'&&$_SERVER['REMOTE_ADDR']!=$ip_of_this_website)){
require('./wp-blog-header.php');
$redis->del($redis_key);
//Secondcase:cacheexistinredis,let'sdisplayit
}elseif($redis->exists($redis_key)){
$html_of_current_page=$redis->get($redis_key);
echo$html_of_current_page;
echo"<!--Thisiscache-->";
//third:anormalvisitorwithoutcache.Anddonotcacheapreviewpagefromthewp-admin:
}elseif($_SERVER['REMOTE_ADDR']!=$ip_of_this_website&&strstr($current_page_url,'preview=true')==false){
require('./wp-blog-header.php');
$html_of_current_page=file_get_contents($current_page_url);
$redis->setex($redis_key,$seconds_of_caching,$html_of_current_page);
echo"<!--Cachehasbeenset-->";
//lastcase:thenormalWordPress.Shouldonlybecalledwithfile_get_contents:
}else{
require('./wp-blog-header.php');
}
//Let'sdisplaysomepagegenerationtime(note:CloudFlaremaystripoutcomments):
$end=microtime();
$t2=(getmicrotime($end)-getmicrotime($start));
if($_SERVER['REMOTE_ADDR']!=$ip_of_this_website){
echo"<!--CachesystembyJimWestergren.Pagegeneratedin".round($t2,5)."seconds.-->";
}
?>

或是直接下载index-with-redis.php
流程2:将所述编码中的IP详细地址换成你网址的IP详细地址
流程3:在.htaccess里将全部出現index.php的地区改成index-with-redis.php,假如你应用的是Nginx则改动nginx.conf中的index.php为index-with-redis.php(并轻载Nginx:killall-sHUPnginx)。
功能测试
1.沒有Redis的状况下,均值主页实行1.614秒,文章内容页0.174秒(无一切缓存文件软件)
2.应用Redis的状况下,均值网页页面实行時间0.00256秒
我已经在我的网站中应用了如上的方式开展加快很长期了,一切运作优良。
别的提议
我的自然环境是NginxPHP-FPMAPCCloudflareRedis.安裝在一个nanoVPS中,无缓存文件软件。
请确定应用了gzip缩小,可加速网站打开速度。
浏览wp-admin
要浏览wp-admin务必应用/wp-admin/index.php替代原先的/wp-admin/.

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/772606.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-03
下一篇 2022-05-03

发表评论

登录后才能评论

评论列表(0条)

保存