redis入门之二

Publish/Subscribe

Redis支持这样一种特性,你可以将数据推到某个信息管道中,然后其它人可以通过订阅这些管道来获取推送过来的信息。

Redis 的 SUBSCRIBE 命令可以让客户端订阅任意数量的频道, 每当有新信息发送到被订阅的频道时, 信息就会被发送给所有订阅指定频道的客户端。

作为例子, 下图展示了频道 channel1 , 以及订阅这个频道的三个客户端 —— client2 、 client5 和 client1 之间的关系:

digraph pubsub_relation {

    rankdir = BT;

    node [style = filled];

    edge [style = bold];

    channel1 [label = "channel1", fillcolor = "#A8E270"];

    node [shape = box, fillcolor = "#95BBE3"];

    client2 [label = "client2"];
    client5 [label = "client5"];
    client1 [label = "client1"];

    client2 -> channel1 [label = "subscribe"];
    client5 -> channel1 [label = "subscribe"];
    client1 -> channel1 [label = "subscribe"];
}

当有新消息通过 PUBLISH 命令发送给频道 channel1 时, 这个消息就会被发送给订阅它的三个客户端:

digraph send_message_to_subscriber {
    
    node [style = filled];

    edge [style = "dashed, bold"];
    
    message [label = "PUBLISH channel1 message", shape = plaintext, fillcolor = "#FADCAD"];

    message -> channel1 [color = "#B22222]"];

    channel1 [label = "channel1", fillcolor = "#A8E270"];

    node [shape = box];

    client2 [label = "client2", fillcolor = "#95BBE3"];
    client5 [label = "client5", fillcolor = "#95BBE3"];
    client1 [label = "client1", fillcolor = "#95BBE3"];

    /*
    client2 -> channel1 [label = "subscribe"];
    client5 -> channel1 [label = "subscribe"];
    client1 -> channel1 [label = "subscribe"];
    */

    channel1 -> client2 [label = "message", color = "#B22222"];
    channel1 -> client5 [label = "message", color = "#B22222"];
    channel1 -> client1 [label = "message", color = "#B22222"];
}

在后面的内容中, 我们将探讨 SUBSCRIBE 和 PUBLISH 命令的实现, 以及这套订阅与发布机制的运作原理。

3.1 订阅信息管道

用一个客户端订阅管道

redis 127.0.0.1:6379> SUBSCRIBE channelone
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channelone"
3) (integer) 1

另一个客户端往这个管道推送信息

redis 127.0.0.1:6379> PUBLISH channelone hello
(integer) 1
redis 127.0.0.1:6379> PUBLISH channelone world
(integer) 1

然后第一个客户端就能获取到推送的信息

redis 127.0.0.1:6379> SUBSCRIBE channelone
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channelone"
3) (integer) 1
1) "message"
2) "channelone"
3) "hello"
1) "message"
2) "channelone"
3) "world"

3.2 按一定模式批量订阅

用下面的命令订阅所有channel开头的信息通道

redis 127.0.0.1:6379> PSUBSCRIBE channel*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "channel*"
3) (integer) 1

在另一个客户端对两个推送信息

redis 127.0.0.1:6379> PUBLISH channelone hello
(integer) 1
redis 127.0.0.1:6379> PUBLISH channeltwo world
(integer) 1

然后在第一个客户端就能收到推送的信息

redis 127.0.0.1:6379> PSUBSCRIBE channel*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "channel*"
3) (integer) 1
1) "pmessage"
2) "channel*"
3) "channelone"
4) "hello"
1) "pmessage"
2) "channel*"
3) "channeltwo"
4) "world"

事务性

Redis本身支持一些简单的组合型的命令,比如以NX结尾命令都是判断在这个值没有时才进行某个命令

redis 127.0.0.1:6379> SET name "John Doe"
OK
redis 127.0.0.1:6379> SETNX name "Dexter Morgan"
(integer) 0
redis 127.0.0.1:6379> GET name
"John Doe"
redis 127.0.0.1:6379> GETSET name "Dexter Morgan"
"John Doe"
redis 127.0.0.1:6379> GET name
"Dexter Morgan"

当然,Redis还支持自定义的命令组合,通过MULTI和EXEC,将几个命令组合起来执行

redis 127.0.0.1:6379> SET counter 0
OK
redis 127.0.0.1:6379> MULTI
OK
redis 127.0.0.1:6379> INCR counter
QUEUED
redis 127.0.0.1:6379> INCR counter
QUEUED
redis 127.0.0.1:6379> INCR counter
QUEUED
redis 127.0.0.1:6379> EXEC
1) (integer) 1
2) (integer) 2
3) (integer) 3
redis 127.0.0.1:6379> GET counter
"3"

你还可以用DICARD命令来中断执行中的命令序列

持久化

Redis的所有数据都存储在内存中,但是他也提供对这些数据的持久化。

1.数据快照

数据快照的原理是将整个Redis中存的所有数据遍历一遍存到一个扩展名为rdb的数据文件中。通过SAVE命令可以调用这个过程。

redis 127.0.0.1:6379> SET name "John Doe"
OK
redis 127.0.0.1:6379> SAVE
OK
redis 127.0.0.1:6379> SET name "Sheldon Cooper"
OK
redis 127.0.0.1:6379> BGSAVE
Background saving started

如果你是使用的brew在Mac OSX上安全的Redis,那么rdb文件会存在如下路径

/usr/local/var/db/redis/dump.rdb

2.Append-Only File(追加式的操作日志记录)

Redis还支持一种追加式的操作日志记录,叫append only file,其日志文件以aof结局,我们一般各为aof文件。要开启aof日志的记录,你需要在配置文件中进行如下设置:

appendonly yes

这时候你所有的操作都会记录在aof日志文件中

redis 127.0.0.1:6379> GET name
(nil)
redis 127.0.0.1:6379> SET name "Ganesh Gunasegaran"
OK
redis 127.0.0.1:6379> EXIT

→ cat /usr/local/var/db/redis/appendonly.aof
*2
$6
SELECT
$1
0
*3
$3
SET
$4
name
$18
Ganesh Gunasegaran

7.管理命令

Redis支持多个DB,默认是16个,你可以设置将数据存在哪一个DB中,不同DB间的数据具有隔离性。也可以在多个DB间移动数据。

redis 127.0.0.1:6379> SELECT 0
OK
redis 127.0.0.1:6379> SET name "John Doe"
OK
redis 127.0.0.1:6379> SELECT 1
OK
redis 127.0.0.1:6379[1]> GET name
(nil)
redis 127.0.0.1:6379[1]> SELECT 0
OK
redis 127.0.0.1:6379> MOVE name 1
(integer) 1
redis 127.0.0.1:6379> SELECT 1
OK
redis 127.0.0.1:6379[1]> GET name
"John Doe"

Redis还能进行一些如下操作,获取一些运行信息

redis 127.0.0.1:6379[1]> DBSIZE
(integer) 1
redis 127.0.0.1:6379[1]> INFO
redis_version:2.2.13
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:64
multiplexing_api:kqueue
......

Redis还支持对某个DB数据进行清除(当然清空所有数据的操作也是支持的)

redis 127.0.0.1:6379> SET name "John Doe"
OK
redis 127.0.0.1:6379> DBSIZE
(integer) 1
redis 127.0.0.1:6379> SELECT 1
OK
redis 127.0.0.1:6379[1]> SET name "Sheldon Cooper"
OK
redis 127.0.0.1:6379[1]> DBSIZE
(integer) 1
redis 127.0.0.1:6379[1]> SELECT 0
OK
redis 127.0.0.1:6379> FLUSHDB
OK
redis 127.0.0.1:6379> DBSIZE
(integer) 0
redis 127.0.0.1:6379> SELECT 1
OK
redis 127.0.0.1:6379[1]> DBSIZE
(integer) 1
redis 127.0.0.1:6379[1]> FLUSHALL
OK
redis 127.0.0.1:6379[1]> DBSIZE
(integer) 0
posted @ 2014-12-03 20:43  程序猿进化之路  阅读(145)  评论(0)    收藏  举报