摘要
Redis是一个开源、支持网络、基于内存、键值对存储数据库,使用ANSI C编写。Redis提供了持久化存储,重启了服务器后Redis依赖快照进行持久化,即使服务器刚开机启动也不会导致负载陡增。Redis缓存比较适合大流量的Wordpress。
1、安装Redis
[root@Legion100 ~]# cd /tmp/
[root@Legion100 /tmp]# wget -c `curl -s http://redis.io/download | awk -F"'" '/2.8.*.tar.gz/{print $4}'`
[root@Legion100 /tmp]# tar xf 2.8.*.tar.gz
[root@Legion100 /tmp]# cd redis-2.8.*
[root@Legion100 /tmp/redis-2.8.20]# if [ `getconf WORD_BIT` == 32 ] && [ `getconf LONG_BIT` == 32 ];then sed -i '1i\CFLAGS= -march=i686' src/Makefile && sed -i 's@^OPT=.*@OPT=-O2 -march=i686@' src/.make-settings;fi
[root@Legion100 /tmp/redis-2.8.20]# make
[root@Legion100 /tmp/redis-2.8.20]# mkdir -p /usr/local/redis/{bin,etc,var}
[root@Legion100 /tmp/redis-2.8.20]# cp -af src/{redis-benchmark,redis-check-aof,redis-check-dump,redis-cli,redis-sentinel,redis-server} /usr/local/redis/bin/
[root@Legion100 /tmp/redis-2.8.20]# cp -a redis.conf /usr/local/redis/etc/
[root@Legion100 /tmp/redis-2.8.21]# echo "export PATH=/usr/local/redis/bin:\$PATH" > /etc/profile.d/redis2.8.sh
[root@Legion100 /tmp/redis-2.8.21]# . /etc/profile.d/redis2.8.sh
[root@Legion100 /tmp/redis-2.8.20]# sed -i 's@pidfile.*@pidfile /var/run/redis.pid@' /usr/local/redis/etc/redis.conf
[root@Legion100 /tmp/redis-2.8.20]# sed -i "s@logfile.*@logfile /usr/local/redis/var/redis.log@" /usr/local/redis/etc/redis.conf
[root@Legion100 /tmp/redis-2.8.20]# sed -i "s@^dir.*@dir /usr/local/redis/var@" /usr/local/redis/etc/redis.conf
[root@Legion100 /tmp/redis-2.8.20]# sed -i 's@daemonize no@daemonize yes@' /usr/local/redis/etc/redis.conf
[root@Legion100 /tmp/redis-2.8.20]# [ -z "`grep ^maxmemory /usr/local/redis/etc/redis.conf`" ] && sed -i 's@maxmemory @maxmemory \nmaxmemory 360000000@' /usr/local/redis/etc/redis.conf
[root@Legion100 /tmp/redis-2.8.20]# wget http://www.dwhd.org/script/Redis-server-init-CentOS -O /etc/init.d/redis-server
[root@Legion100 /tmp/redis-2.8.20]# chmod +x /etc/init.d/redis-server
[root@Legion100 /tmp/redis-2.8.20]# chkconfig --add redis-server
[root@Legion100 /tmp/redis-2.8.20]# chkconfig redis-server on
[root@Legion100 /tmp/redis-2.8.20]# chkconfig --list redis-server
redis-server 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@Legion100 /tmp/redis-2.8.21]# service redis-server start
正在启动 redis-server: [确定]
[root@Legion100 /tmp/redis-2.8.20]# ss -tnlp | grep 6379
LISTEN 0 511 *:6379 *:* users:(("redis-server",48900,5))
LISTEN 0 511 :::6379 :::* users:(("redis-server",48900,4))
[root@Legion100 /tmp/redis-2.8.20]# redis-cli
127.0.0.1:6379> info
# Server
redis_version:2.8.21
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:6b4aad3fc4312fc
redis_mode:standalone
os:Linux 2.6.32-504.12.2.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:13457
run_id:dbece81080ec83837419cf7f157ed12772e52e65
tcp_port:6379
uptime_in_seconds:2412
uptime_in_days:0
hz:10
lru_clock:7507826
config_file:/usr/local/redis/etc/redis.conf
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:1216912
used_memory_human:1.16M
used_memory_rss:4407296
used_memory_peak:1216912
used_memory_peak_human:1.16M
used_memory_lua:36864
mem_fragmentation_ratio:3.62
mem_allocator:jemalloc-3.6.0
# Persistence
loading:0
rdb_changes_since_last_save:2
rdb_bgsave_in_progress:0
rdb_last_save_time:1433570558
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
# Stats
total_connections_received:59
total_commands_processed:92
instantaneous_ops_per_sec:0
total_net_input_bytes:437180
total_net_output_bytes:1030842
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:57
keyspace_misses:7
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:212
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:0.85
used_cpu_user:0.36
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
# Keyspace
db0:keys=1,expires=0,avg_ttl=0
127.0.0.1:6379>
[root@Legion100 /tmp/redis-2.8.21]#
2、用Predis.php作为Redis的PHP客户端
#进入你的wordpress根目录
[root@Legion100 /tmp/redis-2.8.20]# cd /home/wwwroot/www.dwhd.org/
[root@Legion100 www.dwhd.org]# wget http://www.dwhd.org/script/phpredis -O predis.php
[root@Legion100 www.dwhd.org]# chmod www.www predis.php
[root@Legion100 www.dwhd.org]#
3、安装Redis前端缓存的PHP脚本
[root@Legion100 www.dwhd.org]# mv index.php index.php_backup_`date +%F`
[root@Legion100 www.dwhd.org]# wget http://www.dwhd.org/script/index-with-redis -O index.php
[root@Legion100 www.dwhd.org]# chown www.www index.php
4、Wordpress删除Redis页面缓存的方法
1)、删除某一个页面的缓存:发表评论、按下F5刷新、在URL后面加上?r=y回车。
2)、删除整站页面缓存:登录到Wordpress后台,在任意URL后面加上?r=y回车。
3)、更新文章时自动刷新首页缓存:在你的Wordpress的Function.php中加入以下代码
// 更新文章时自动刷新首页缓存 redis
function newPostRefresh() {
$temp=file_get_contents("http://www.dwhd.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');
设置首页定时刷新
[root@mail www.dwhd.org]# echo "*/5 * * * * `which curl` http://www.dwhd.org/?r=y >/dev/null 2>&1" /var/spool/cron/root