Redis是一個高級的keyvalue存儲系統(tǒng),類似memcached,所有內(nèi)容都存在內(nèi)存中,因此每秒鐘可以超過10萬次GET操作。
我下面提出的解決方案是在Redis中緩存所有輸出的HTML內(nèi)容而無需再讓W(xué)ordPress重復(fù)執(zhí)行頁面腳本。這里使用Redis代替Varnish設(shè)置簡單,而且可能更快。
安裝Redis
如果你使用的是Debian或者衍生的操作系統(tǒng)可使用如下命令安裝Redis
aptgetinstallredisserver
或者閱讀安裝指南
使用Predis作為Redis的PHP客戶端
你需要一個客戶端開發(fā)包以便PHP可以連接到Redis服務(wù)上。
這里我們推薦Predis.上傳predis到WordPress的根目錄。
前端緩存的PHP腳本
步驟1 在WordPress的根目錄創(chuàng)建新文件withredis,內(nèi)容如下
- Changethesetwovariables:
- $seconds_of_caching=60 60 24 7;7days.
- $ip_of_this_website='204.62.14.112';
- ThisfileiswrittenbyJimWestergren,copyrightallrightsreserved.
- Seemorehere:jimwestergrenwordpresswithredisasafrontendcache
- Thecodeisfreeforeveryonetousehowtheywantbutpleasementionmynameandlinktomyarticlewhenwritingaboutthis.
- Change$ip_of_this_websitetotheIPofyourwebsiteabove.
- Add?refresh=yestotheendofaURLtorefreshit'scache
- Youcanalsoentertheredisclientviathecommandpromptwiththecommand"rediscli"andthenremoveallcachewiththecommand"flushdb".
- VerynecessaryifyouuseCloudfare:
- ifisset$_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;
- returnfloat$usec+float$sec;
- }
- $start=microtime;
- InitiateredisandthePHPclientforredis:
- include"predis";
- $redis=newPredis/Client'';
- fewvariables:
- $current_page_url="".$_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
- ifisset$_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'.wpblogheader';
- $redisdel$redis_key;
- Secondcase:cacheexistinredis,let'sdisplayit
- }elseif$redisexists$redis_key{
- $html_of_current_page=$redisget$redis_key;
- echo$html_of_current_page;
- echo"!Thisiscache
- third:anormalvisitorwithoutcache.Anddonotcacheapreviewpagefromthewpadmin:
- }elseif$_SERVER['REMOTE_ADDR']!=$ip_of_this_websitestrstr$current_page_url,'preview=true'==false{
- require'.wpblogheader';
- $html_of_current_page=file_get_contents$current_page_url;
- $redissetex$redis_key,$seconds_of_caching,$html_of_current_page;
- echo"!Cachehasbeenset
- lastcase:thenormalWordPress.Shouldonlybecalledwithfile_get_contents:
- }else{
- require'.wpblogheader';
- }
- Let'sdisplaysomepagegenerationtimenote:CloudFlaremaystripo utcomments:
- $end=microtime;
- $t2=getmicrotime$endgetmicrotime$start;
- if$_SERVER['REMOTE_ADDR']!=$ip_of_this_website{
- //Vevb.com
- echo"!CachesystembyJimWestergren.Pagegeneratedin".round$t2,5."seconds.
- }
或者直接下載withredis
步驟2 將上述代碼中的IP替換成你的IP
步驟3 在.htaccess中將所有出現(xiàn)的地方改為withredis,如果你使用的是Nginx則修改nginx.conf中的為withredis并重載Nginx killallsHUPnginx。
性能測試
1.沒有Redis的情況下,平均執(zhí)行1.614秒,頁0.174秒無任何緩存插件
2.使用Redis的情況下,平均頁面執(zhí)行時間0.00256秒
我已經(jīng)在我的博客中使用了如上的方法進行加速很長時間了,一切運行良好。
其他建議
我的環(huán)境是Nginx+PHPFPM+APC+Cloudflare+Redis.安裝在一個nanoVPS中,無緩存插件。
請確認(rèn)使用了gzip壓縮,可加快訪問速度。
訪問wpadmin
要訪問wpadmin必須使用wpadmin代替原來的wpadmin.
新聞熱點
疑難解答
圖片精選