wordpress启动Redis缓存加速

wordpress启动Redis缓存加速

Redis是一个高级的key-value存储系统,类似memcached,所有内容都存在内存中,因此每秒钟可以超过10万次GET操作。
我下面提出的解决方案是在Redis缓存所有输出的HTML 内容而无需再让WordPress重复执行页面脚本。这里使用Redis代替Varnish设置简单,而且可能更快。

和memcache一样redis也分客户端(php插件)和服务端

安装Redis服务端
参考://linuxeye.com/308.html

安装Redis php客户端

cd /root/lnmp/src
wget http://pecl.php.net/get/redis-2.2.3.tgz
tar xzf redis-2.2.3.tgz
cd redis-2.2.3
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
sed -i 's@^extension_dir\(.*\)@extension_dir\1\nextension = "redis.so"@' /usr/local/php/etc/php.ini
service php-fpm restart

lnmp一键安装包》已经安装了,只需要配置,请看下面

使wordpress支持Redis
注意:请关闭所有其他缓存插件,原作者改进了插件,以前的翻译的文章不适用。如果有问题请加QQ群: 235258658
你需要一个客户端开发包以便PHP可以连接到redis服务端
这里我们推荐predis. 加入WordPress的根目录,执行下面

wget http://uploads.staticjw.com/ji/jim/predis.php
chown www.www ./predis.php

前端缓存的PHP脚本,加入WordPress的根目录,执行下面

wget https://gist.githubusercontent.com/JimWestergren/3053250/raw/d9e279e31cbee4a1520f59108a4418ae396b2dde/index-with-redis.php
chown www.www ./index-with-redis.php

根据自己需求修改index-with-redis.php,修改如下:

$cf = 0;                        // set to 1 if you are using cloudflare
$debug = 1;                     // set to 1 if you wish to see execution time and cache actions
$display_powered_by_redis = 0;  // set to 1 if you want to display a powered by redis message with execution time, see below

替换index.php

mv index.php index.php_bk #替换到原来的index.php,也可以改nginx伪静态规则
mv index-with-redis.php index.php

缓存问题
index-with-redis.php中有注释

Appending a ?c=y to a url deletes the entire cache of the domain, only works when you are logged in
Appending a ?r=y to a url deletes the cache of that url
Submitting a comment deletes the cache of that page
Refreshing (f5) a page deletes the cache of that page

  • 登录后台网站url后面加上?c=y即可刷新整个网站
  • 可以在网站页面后面加上?r=y即可手工刷新
  • 提交评论会自动刷新页面
  • 刷新(f5)页面也可以刷新页面

测试结果
可查看本博客网页源码,我的数据是

no cached: 0.3665
this is a cached: 0.00145

使discuz支持redis,修改config/config_global.php

$_config['memory']['redis']['server'] = '127.0.0.1';

参考:http://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/

Thu Aug 22 21:47:05 CST 2013

【AD】炭云:768元/年/1GB内存/20GB SSD空间/2TB流量/500Mbps-1Gbps端口/独立IPv4/KVM/广州移动

【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps