加速WordPress技巧:Redis緩存輸出的HTML頁面
發(fā)布時(shí)間:2012-07-11 15:28:51 作者:佚名
我要評(píng)論

Redis是一個(gè)高級(jí)的key-value存儲(chǔ)系統(tǒng),類似memcached,所有內(nèi)容都存在內(nèi)存中,因此每秒鐘可以超過10萬次GET操作。
Redis是一個(gè)高級(jí)的key-value存儲(chǔ)系統(tǒng),類似memcached,所有內(nèi)容都存在內(nèi)存中,因此每秒鐘可以超過10萬次GET操作。
我下面提出的解決方案是在Redis中緩存所有輸出的HTML 內(nèi)容而無需再讓W(xué)ordPress重復(fù)執(zhí)行頁面腳本。這里使用Redis代替Varnish設(shè)置簡(jiǎn)單,而且可能更快。
安裝 Redis
如果你使用的是 Debian 或者衍生的操作系統(tǒng)可使用如下命令安裝 Redis:
apt-get install redis-server
或者閱讀 安裝指南
使用 Predis 作為 Redis 的 PHP 客戶端
你需要一個(gè)客戶端開發(fā)包以便 PHP 可以連接到 Redis 服務(wù)上。
這里我們推薦 Predis. 上傳 predis.php 到 WordPress 的根目錄。
前端緩存的PHP腳本
步驟1:在WordPress 的根目錄創(chuàng)建新文件 index-with-redis.php ,內(nèi)容如下:
<?php
// Change these two variables:
$seconds_of_caching = 60*60*24*7; // 7 days.
$ip_of_this_website = '204.62.14.112';
/*
- This file is written by Jim Westergren, copyright all rights reserved.
- See more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
- The code is free for everyone to use how they want but please mention my name and link to my article when writing about this.
- Change $ip_of_this_website to the IP of your website above.
- Add ?refresh=yes to the end of a URL to refresh it's cache
- You can also enter the redis client via the command prompt with the command "redis-cli" and then remove all cache with the command "flushdb".
*/
// Very necessary if you use Cloudfare:
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
// This is from WordPress:
define('WP_USE_THEMES', true);
// Start the timer:
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
$start = microtime();
// Initiate redis and the PHP client for redis:
include("predis.php");
$redis = new Predis\Client('');
// few variables:
$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);
// This first case is either manual refresh cache by adding ?refresh=yes after the URL or somebody posting a comment
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);
// Second case: cache exist in redis, let's display it
} else if ($redis->exists($redis_key)) {
$html_of_current_page = $redis->get($redis_key);
echo $html_of_current_page;
echo "<!-- This is cache -->";
// third: a normal visitor without cache. And do not cache a preview page from the wp-admin:
} else if ($_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 "<!-- Cache has been set -->";
// last case: the normal WordPress. Should only be called with file_get_contents:
} else {
require('./wp-blog-header.php');
}
// Let's display some page generation time (note: CloudFlare may strip out comments):
$end = microtime();
$t2 = (getmicrotime($end) - getmicrotime($start));
if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website) {
echo "<!-- Cache system by Jim Westergren. Page generated in ".round($t2,5)." seconds. -->";
}
?>
或者直接下載 index-with-redis.php
步驟2:將上述代碼中的 IP 地址替換成你網(wǎng)站的 IP 地址
步驟3:在.htaccess 中將所有出現(xiàn) index.php 的地方改為 index-with-redis.php ,如果你使用的是 Nginx 則修改 nginx.conf 中的 index.php 為 index-with-redis.php(并重載 Nginx : killall -s HUP nginx)。
性能測(cè)試
1.沒有Redis 的情況下,平均首頁執(zhí)行1.614 秒,文章頁0.174 秒(無任何緩存插件)
2.使用Redis 的情況下,平均頁面執(zhí)行時(shí)間0.00256秒
我已經(jīng)在我的博客中使用了如上的方法進(jìn)行加速很長(zhǎng)時(shí)間了,一切運(yùn)行良好。
其他建議
我的環(huán)境是Nginx + PHP-FPM + APC + Cloudflare + Redis. 安裝在一個(gè) nano VPS 中,無緩存插件。
請(qǐng)確認(rèn)使用了gzip壓縮,可加快訪問速度。
訪問 wp-admin
要訪問 wp-admin 必須使用 /wp-admin/index.php 代替原來的 /wp-admin/.
我下面提出的解決方案是在Redis中緩存所有輸出的HTML 內(nèi)容而無需再讓W(xué)ordPress重復(fù)執(zhí)行頁面腳本。這里使用Redis代替Varnish設(shè)置簡(jiǎn)單,而且可能更快。
安裝 Redis
如果你使用的是 Debian 或者衍生的操作系統(tǒng)可使用如下命令安裝 Redis:
apt-get install redis-server
或者閱讀 安裝指南
使用 Predis 作為 Redis 的 PHP 客戶端
你需要一個(gè)客戶端開發(fā)包以便 PHP 可以連接到 Redis 服務(wù)上。
這里我們推薦 Predis. 上傳 predis.php 到 WordPress 的根目錄。
前端緩存的PHP腳本
步驟1:在WordPress 的根目錄創(chuàng)建新文件 index-with-redis.php ,內(nèi)容如下:
復(fù)制代碼
代碼如下:<?php
// Change these two variables:
$seconds_of_caching = 60*60*24*7; // 7 days.
$ip_of_this_website = '204.62.14.112';
/*
- This file is written by Jim Westergren, copyright all rights reserved.
- See more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
- The code is free for everyone to use how they want but please mention my name and link to my article when writing about this.
- Change $ip_of_this_website to the IP of your website above.
- Add ?refresh=yes to the end of a URL to refresh it's cache
- You can also enter the redis client via the command prompt with the command "redis-cli" and then remove all cache with the command "flushdb".
*/
// Very necessary if you use Cloudfare:
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
// This is from WordPress:
define('WP_USE_THEMES', true);
// Start the timer:
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
$start = microtime();
// Initiate redis and the PHP client for redis:
include("predis.php");
$redis = new Predis\Client('');
// few variables:
$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);
// This first case is either manual refresh cache by adding ?refresh=yes after the URL or somebody posting a comment
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);
// Second case: cache exist in redis, let's display it
} else if ($redis->exists($redis_key)) {
$html_of_current_page = $redis->get($redis_key);
echo $html_of_current_page;
echo "<!-- This is cache -->";
// third: a normal visitor without cache. And do not cache a preview page from the wp-admin:
} else if ($_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 "<!-- Cache has been set -->";
// last case: the normal WordPress. Should only be called with file_get_contents:
} else {
require('./wp-blog-header.php');
}
// Let's display some page generation time (note: CloudFlare may strip out comments):
$end = microtime();
$t2 = (getmicrotime($end) - getmicrotime($start));
if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website) {
echo "<!-- Cache system by Jim Westergren. Page generated in ".round($t2,5)." seconds. -->";
}
?>
或者直接下載 index-with-redis.php
步驟2:將上述代碼中的 IP 地址替換成你網(wǎng)站的 IP 地址
步驟3:在.htaccess 中將所有出現(xiàn) index.php 的地方改為 index-with-redis.php ,如果你使用的是 Nginx 則修改 nginx.conf 中的 index.php 為 index-with-redis.php(并重載 Nginx : killall -s HUP nginx)。
性能測(cè)試
1.沒有Redis 的情況下,平均首頁執(zhí)行1.614 秒,文章頁0.174 秒(無任何緩存插件)
2.使用Redis 的情況下,平均頁面執(zhí)行時(shí)間0.00256秒
我已經(jīng)在我的博客中使用了如上的方法進(jìn)行加速很長(zhǎng)時(shí)間了,一切運(yùn)行良好。
其他建議
我的環(huán)境是Nginx + PHP-FPM + APC + Cloudflare + Redis. 安裝在一個(gè) nano VPS 中,無緩存插件。
請(qǐng)確認(rèn)使用了gzip壓縮,可加快訪問速度。
訪問 wp-admin
要訪問 wp-admin 必須使用 /wp-admin/index.php 代替原來的 /wp-admin/.
相關(guān)文章
CyberPanel安裝WordPress并配置偽靜態(tài)規(guī)則
下面教你如何在 CyberPanel安裝WordPress以及配置偽靜態(tài),需要的朋友可以參考下2023-12-27- 這篇文章主要介紹了wordpress無法安裝更新主題插件的解決辦法,需要的朋友可以參考下2020-12-27
WordPress必備數(shù)據(jù)庫SQL查詢語句整理
發(fā)現(xiàn)幾條比較實(shí)用的,適合 WordPress 實(shí)用的SQL語句。于是就趕緊收集分享出來了,需要的朋友可以參考下2017-09-23wordpress在安裝使用中出現(xiàn)404、403、500及502問題的分析與解決方法
wordpress是很多新手站長(zhǎng)搭建個(gè)人博客最喜愛的程序,但是最近在使用WordPress的時(shí)候遇到了一些問題,所以想著將遇到問題總結(jié)分享出來,下面這篇文章主要給大家介紹了關(guān)于wo2017-08-11WordPress取消英文標(biāo)點(diǎn)符號(hào)自動(dòng)替換中文標(biāo)點(diǎn)符號(hào)的優(yōu)雅方法
這篇文章主要介紹了WordPress取消英文標(biāo)點(diǎn)符號(hào)自動(dòng)替換中文標(biāo)點(diǎn)符號(hào)的優(yōu)雅方法,需要的朋友可以參考下2017-04-04- 這篇文章主要給大家介紹了wordpress自定義上傳文件類型的方法,如WordPress默認(rèn)允許上傳 .exe 后綴名的可運(yùn)行文件,那么我們?cè)趺唇褂脩粼赪ordPress后臺(tái)發(fā)表文章時(shí)上傳 .e2016-12-19
- 大家可能發(fā)現(xiàn)了當(dāng)實(shí)現(xiàn)了前端用戶中心,后臺(tái)控制面板就失去了作用,那么限制其他用戶進(jìn)入后臺(tái)控制面板就很有必要了!那么我們要怎么做呢?通過下面這篇文章分享的方法后,只2016-12-19
WordPress實(shí)現(xiàn)回復(fù)文章評(píng)論后發(fā)送郵件通知的功能
這篇文章主要介紹了WordPress實(shí)現(xiàn)回復(fù)文章評(píng)論后發(fā)送郵件通知的功能,涉及wordpress針對(duì)評(píng)論與郵件的相關(guān)操作技巧,需要的朋友可以參考下2016-10-11WordPress使用自定義文章類型實(shí)現(xiàn)任意模板的方法
這篇文章主要介紹了WordPress使用自定義文章類型實(shí)現(xiàn)任意模板的方法,可通過自定義文章類型來實(shí)現(xiàn)任意模版的使用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-10-11WordPress后臺(tái)地址被改導(dǎo)致無法登陸后臺(tái)的簡(jiǎn)單解決方法
這篇文章主要介紹了WordPress后臺(tái)地址被改導(dǎo)致無法登陸后臺(tái)的簡(jiǎn)單解決方法,簡(jiǎn)單分析了后臺(tái)無法登陸的原因與相應(yīng)的解決方法,涉及針對(duì)wordpress配置項(xiàng)的簡(jiǎn)單修改,需要的朋友2016-10-11