收藏本站|设为首页

您现在的位置: 首页 > 新闻中心 > 建站经验 > 详细内容

让WordPress使用Redis缓存来进行加速

2012-07-06 10:06 来源: 卓杰科技 www.zhuojie.cc [ ]

// This first case is either manual refresh cache by adding ?refresh=yes after the URL or somebody posting a comment

Redis是一个高级的key-value存储系统,近似memcached,所有内容都存在内存中,是以每秒钟可以跨越10万次GET操作。

apt-get install redis-server

使用 Predis 作为 Redis 的 PHP 客户端


- Add ?refresh=yes to the end of a URL to refresh it's cache

我下面提出的解决方案是在Redis中缓存所有输出的HTML 内容而无需再让WordPress一再执行页面剧本。这里使用Redis庖代Varnish设置简单,而且可能更快。

你需要一个客户端开发包以便 PHP 可以毗连到 Redis 处事上。

这里我们举荐 Predis. 上传 predis.php 到 WordPress 的根目录。

轨范1:在WordPress 的根目录建树新文件 index-with-redis.php ,内容如下:

<?php

$start = microtime();

$seconds_of_caching = 60*60*24*7; // 7 days.


?>

/*

- 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.
}
- 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:

} else {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}

define('WP_USE_THEMES', true);

function getmicrotime($t) {
return ((float)$usec + (float)$sec);
}


list($usec, $sec) = explode(" ",$t);

echo $html_of_current_page;

// Initiate redis and the PHP client for redis:

include("predis.php");
$redis = new Predis\Client('');

$current_page_url = "?$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];


$t2 = (getmicrotime($end) - getmicrotime($start));

请确认使用了gzip压缩,可加速访谒速度。

$current_page_url = str_replace('?refresh=yes', '', $current_page_url);

$redis_key = md5($current_page_url);

安装 Redis

// Change these two variables:

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');

// This is from WordPress:


$html_of_current_page = file_get_contents($current_page_url);
$redis->del($redis_key);

// Second case: cache exist in redis, let's display it

if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {

$html_of_current_page = $redis->get($redis_key);

// 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) {
$redis->setex($redis_key, $seconds_of_caching, $html_of_current_page);

echo "<!-- This is cache -->";

// last case: the normal WordPress. Should only be called with file_get_contents:


require('./wp-blog-header.php');

// Let's display some page generation time (note: CloudFlare may strip out comments):

$ip_of_this_website = '204.62.14.112';

$end = microtime();

前端缓存的PHP剧本


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

如不美观你使用的是 Debian 或者衍生的操作系统可使用如下呼吁安装 Redis:


require('./wp-blog-header.php');

} else if ($redis->exists($redis_key)) {

WordPress plus Redis = Love

// Start the timer:


}

轨范2:将上述代码中的 IP 地址替代成你网站的 IP 地址

轨范3:在.htaccess 中将所有呈现 index.php 的处所改为 index-with-redis.php ,如不美观你使用的是 Nginx 则改削 nginx.conf 中的 index.php 为 index-with-redis.php(并重载 Nginx : killall -s HUP nginx)。

其他建议

1.没有Redis 的情形下,平均首页执行1.614 秒,文章页0.174 秒(无任何缓存插件)

2.使用Redis 的情形下,平均页面执行时刻0.00256秒

我已经在我的博客中使用了如上的体例进行加速很长时刻了,一切运行精采。


echo "<!-- Cache has been set -->";

我的情形是Nginx + PHP-FPM + APC + Cloudflare + Redis. 安装在一个 nano VPS 中,无缓存插件。

// few variables:

访谒 wp-admin