WordPress插件:Redis Object Cache 缓存

2016年6月21日 | 分类: 【技术】, 【源码】

应用于WordPress的插件Redis Object Cache是一个基于Redis的对象缓存后台:支持Predis、PhpRedis、HHVM、replication和clustering。”

参考:https://wordpress.org/support/topic/works-like-a-charm-628#post-

“需要安装Redis server和PHP Redis(可选)。当选用插件Redis Object Cache时,因为插件中已经内置predis库,所以不必安装PHP Redis。只是因为PHP Redis会添加PECL库,比predis库性能更好。”

参考:https://mos.meituan.com/library/29/how-to-setup-redis-to-speed-up-wordpress/

“Redis官方提供了多种客户端可以连接到Redis服务器,Redis Object Cache插件自身支持hhvm, pecl 和 predis这三种,推荐使用pecl,安装速度快。”

参考:https://amon.org/why-we-use-redis

安装Redis:

参考: https://amon.org/redis.htm

安装插件:Redis Object Cache

官网:https://wordpress.org/plugins/redis-cache

1. 下载压缩包并解压到 wordpress/wp-content/plugins 中。

下载:https://downloads.wordpress.org/plugin/redis-cache.1.3.4.zip

2. 在WordPress中激活插件。

进入 settings > Redis 启动Redis Object Cache。

点击按钮“Enable Object Cache”,显示:

Object Cache could not be enabled.

查看Apache日志:

[Wed Jun 22 12:41:54.221113 2016] [:error] [pid 4554:tid 139930751719168] [client 123.120.171.3:58832] PHP Warning:  copy(/usr/local/apache2/htdocs/amon.org/wp-content/object-cache.php): failed to open stream: Permission denied in /usr/local/apache2/htdocs/amon.org/wp-admin/includes/class-wp-filesystem-direct.php on line 257, referer: https://amon.org/wp-admin/options-general.php?page=redis-cache

原因是内部程序没有权限执行拷贝,所以可以手工把 includes文件夹下的object-cache.php 拷贝到 /wp-content/ ,或者 将 /wp-content/ 权限设置为 777 。

参考:https://wordpress.org/support/topic/object-cache-could-not-be-enabled

再次点击按钮“Enable Object Cache”,显示:

Overview
Status: 	Connected
Client: 	Predis (v1.0.3)

如果想关闭此插件,点击“Disable Object Cache”之后,显示:

Object Cache could not be disabled.

原因是内部程序没有权限执行删除,所以可以手工把 /wp-content/ 文件夹下的object-cache.php 删除,或者 将 /wp-content/ 权限设置为 777 。

参考:https://wordpress.org/support/topic/great-plugin-10212?replies=2#post-

此插件作者建议在WordPress上安装Bug追踪插件 Debug Bar (https://wordpress.org/plugins/debug-bar/ ),另外专文研究。

多站点共用redis服务器配置方式

参考:https://wordpress.org/support/topic/once-i-install-redis-cache-for-2-wordpress-sites-they-conflicted/
参考:https://wordpress.org/support/topic/two-different-clients-reported/

You need to use separate databases, or set a WP_CACHE_KEY_SALT.

云端redis服务器配置方式

默认情况下,Redis Object Cache插件会默认通过TCP协议连接127.0.0.1:6379并且选择Redis数据库db0,但如果redis服务器在云端,需要配置一下。

编辑WordPress配置文件 /wp-config.php :

在That’s all, stop editing! Happy blogging.这一行前添加如下内容:

define('WP_REDIS_CLIENT', 'pecl');
define('WP_REDIS_SCHEME', 'tcp');
define('WP_REDIS_HOST', '$Your.Redis.IP');
define('WP_REDIS_DATABASE', '0');
define('WP_CACHE_KEY_SALT', 'wp_');
define('WP_REDIS_MAXTTL', '86400');

/* That's all, stop editing! Happy blogging. */

配置完成后,进入WordPress控制台,“工具”->“Redis”

检查配置无误后,点击“Enable Object Cache”

Redis Status显示为Connected则表示连接成功。

验证缓存服务可用性

使用命令行连接至Redis server

redis-cli -h $Your.Redis.IP

查看缓存是否已经成功

> info

输出:

...
# Keyspace
db0:keys=3404,expires=371,avg_ttl=80334206

可见db0中已经有了数据。

我们可以查看一下:

>keys *

输出:

  1) "wp_:posts:92"
  2) "wp_:post_tag_relationships:148"
  3) "wp_:post_meta:86"
  4) "wp_:post_tag:7"
  5) "wp_:posts:46"
  6) "wp_user_meta:1"
  7) "wp_:post_tag_relationships:142"
  8) "wp_:post_tag_relationships:178"
  9) "wp_:posts:99"
 10) "wp_:options:alloptions"

证明缓存已经成功了。

三. B 非插件方法使用Predis.php【参考】

用Predis.php作为Redis的PHP客户端

进入wordpress根目录,下载Predis.php:

cd /usr/local/apache2/htdocs/amon.org/
wget http://www.dwhd.org/script/phpredis -O predis.php
chmod www.www predis.php
[shell]

安装Redis前端缓存的PHP脚本

[shell]
mv index.php index.php_backup_`date +%F`
wget http://www.dwhd.org/script/index-with-redis -O index.php
chown www.www index.php

WordPress删除Redis页面缓存的方法

1. 删除某一个页面的缓存:发表评论、按下F5刷新、在URL后面加上?r=y回车。
2. 删除整站页面缓存:登录到Wordpress后台,在任意URL后面加上?r=y回车。
3. 更新文章时自动刷新首页缓存:在你的Wordpress的Function.php中加入以下代码

function newPostRefresh() {
        $temp=file_get_contents("https://amon.org/?r=y");//网址换成自己的
}
add_action('publish_post', 'newPostRefresh');
add_action('edit_post', 'newPostRefresh');
add_action('delete_post', 'newPostRefresh');
add_action('comment_post', 'newPostRefresh');
add_action('edit_comment', 'newPostRefresh');
add_action('delete_comment', 'newPostRefresh');
add_action('wp_set_comment_status', 'newPostRefresh');
add_action('switch_theme', 'newPostRefresh');

设置首页定时刷新

echo "*/5 * * * * `which curl` https://amon.org/?r=y  >/dev/null 2>&1" /var/spool/cron/root

四. 性能测试

当各个环境部署就绪,如何测试配上Redis之后WordPress的性能提升情况?

参考:http://www.saunix.cn/33

五. Redis集群

还没开始研究。

参考:http://www.oschina.net/question/12_30289
参考:http://www.th7.cn/db/nosql/201510/135271.shtml

六. 参考资料:

参考:https://github.com/JasonLai256/the-little-redis-book/blob/master/cn/redis.md
参考:https://www.dwhd.org/20150607_143919
参考:https://mos.meituan.com/library/29/how-to-setup-redis-to-speed-up-wordpress/
参考:http://blog.csdn.net/moguibeijing/article/details/17769137
参考:http://shadowlong.me/blog-view-ZqMzc7
参考:http://www.milletblog.com/2016/04/linux-%e5%ae%89%e8%a3%85redis-%e9%83%a8%e7%bd%b2-%e5%90%af%e5%8a%a8-%e5%81%9c%e6%ad%a2/

七. 报错及解决方法:

以上步骤中已经解决这些报错,以下资料仅供参考。

如果要确保服务器重启后设置失效,需要在/etc/rc.local中加入这些命令。

启动redis过程中会报错。

问题1:

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

解决方法:

参考:http://it.taocms.org/04/7505.htm

echo 511 > /proc/sys/net/core/somaxconn

问题2:

WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1′ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1′ for this to take effect.

解决方法:

参考:http://it.taocms.org/04/7505.htm
参考:http://www.cnblogs.com/whendream/p/4228885

echo 1 > /proc/sys/vm/overcommit_memory

或者:

vi /etc/sysctl.conf

编辑:

vm.overcommit_memory = 1

然后应用生效:

sysctl –p

问题3:

参考:http://antirez.com/news/84

 WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 16183:M 22 Jun 03:13:11.853 * The server is now ready to accept connections on port 6379

解决方法:

echo never > /sys/kernel/mm/transparent_hugepage/enabled

问题4:

参考:http://ask.csdn.net/questions/256048
参考:http://www.cnblogs.com/moxiaoan/p/5737430
参考:https://segmentfault.com/q/1010000004110780?sort=created

php7.0.5安装redis扩展,phpinfo还是不显示redis。但其他都正常。

因为phpredis 有单独的php7的分支。需要在configure之前多一个步骤:git checkout php7