wordpress启动Redis缓存加速

Linux116阅读模式

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

 
  • 本文由 yeho 发表于 2013-08-22
  • 转载请务必保留本文链接:https://linuxeye.com/354.html
Linux

Nginx反向代理永久性缓存

Nginx缓存简介 Nginx缓存方式有两种: 永久性的缓存:这种缓存若不手动删除,该缓存文件会一直生效,因此,永久缓存只是用于缓存网站中几乎不会更改的内容; 临时缓存:这种缓存是根据请求连接进行哈希...
    • cc
      cc

      怎么搞的 我全站500了。是不是 和其它插件有冲突哈。
      我用了Memcache Bached 做缓存。

      • 小C博客
        小C博客

        博主你好!有没有最新的wordpress redis使用方法

        • Seon
          Seon

          还适用现版本的WP么?还有PHP7。

          • hong
            hong

            这个现在还适用么?对php7.0兼容如何?或者说php7.0还有必要使用这个么

              • yeho
                yeho

                @ hong 写的脚本不一定支持php7

              • hong
                hong

                http://pecl.php.net/package/redis 这里的版本还不支持PHP7.
                要用的话,应该下载这个:
                https://github.com/phpredis/phpredis/archive/php7.zip

                • PHP程序员雷雪松
                  PHP程序员雷雪松

                  学习学习!!!

                匿名

                发表评论

                匿名网友
                确定

                取消

                拖动滑块以完成验证