欢迎大家加入 459479177QQ群进行交流


本次主要介绍Redis的发布订阅、事务、脚本、连接、HyperLogLog


一、发布订阅

1>psubscribe,订阅一个或多个指定的频道

Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "tv1"
3) (integer) 1
127.0.0.1:6379> psubscribe tv2 tv3                \\指定订阅2个频道tv2 tv3
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "tv2"
3) (integer) 1
1) "psubscribe"
2) "tv3"
3) (integer) 2

2>pubsub查看发布订阅系统

127.0.0.1:6379> PUBSUB channels
(empty list or set)

3>publish给指定channel发送message

\\先订阅一个channel,然就一直监听着
127.0.0.1:6379> psubscribe tv1
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "tv1"
3) (integer) 1
1) "pmessage"
2) "tv1"
3) "tv1"
4) "test"
\\使用publish指定channel发送message
127.0.0.1:6379> publish www.jinr.com  "test"
(integer) 1

4>punsunbscribe退订所有给定的频道

redis 127.0.0.1:6379> PUNSUBSCRIBE mychannel     \\官方的例子
1) "punsubscribe"
2) "a"
3) (integer) 1

5>sunbscribe订阅一个或多个频道信息

redis 127.0.0.1:6379> SUBSCRIBE mychannel         \\官方给 例子
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "mychannel"
3) (integer) 1
1) "message"
2) "mychannel"
3) "a"

6>unsubscribe指定退订的频道

redis 127.0.0.1:6379> UNSUBSCRIBE mychannel         \\官方的例子
1) "unsubscribe"
2) "a"
3) (integer) 0

二、事务

1>multi标记事务开始

127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name sunshine
QUEUED
127.0.0.1:6379> set name sunshineboy
QUEUED

2>exec执行所有事务块内的命令

127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name sunshine
QUEUED
127.0.0.1:6379> set name sunshineboy
QUEUED
127.0.0.1:6379> exec
1) OK
2) OK
127.0.0.1:6379> get name 
"sunshineboy"

3>discard取消所有事务块内的命令

127.0.0.1:6379> get name 
"sunshineboy"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name tom
QUEUED
127.0.0.1:6379> exec
1) OK
127.0.0.1:6379> get name
"tom"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name jeery
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> get name
"tom"

4>watch乐观锁监视一个或多个key,如果在这个事务之前执行则打断该事务

127.0.0.1:6379> watch name 
OK
127.0.0.1:6379> set name jeery            \\在执行multi事务前
OK
127.0.0.1:6379> multi 
OK
127.0.0.1:6379> set name sunshine
QUEUED
127.0.0.1:6379> exec
(nil)
127.0.0.1:6379> get name
"jeery"

5>unwatch取消乐观锁,停止监视一个或多个key

127.0.0.1:6379> watch name 
OK
127.0.0.1:6379> unwatch
OK


三、脚本

1>eval执行lua脚本

127.0.0.1:6379> eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first second1
1) "key1"
2) "key2"
3) "first"
4) "second1"
127.0.0.1:6379> eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first2 second1
1) "key1"
2) "key2"
3) "first2"
4) "second1"
127.0.0.1:6379> eval "return {KEYS[1],ARGV[1]}" 1  key1 tom
1) "key1"
2) "tom"


2>script load添加脚本到缓存,但不执行

127.0.0.1:6379> script load "return sunshine"
"2e985836734751e115ec1eb4762d68ff62b9ca26"

3>script 杀死一个lua脚本

redis 127.0.0.1:6379> SCRIPT KILL
OK

4>script flush从缓存移除所有脚本

127.0.0.1:6379> script flush
OK

5>script exsits 查看一个缓存是否存在

127.0.0.1:6379> script load "return 'sunshine'"
"f3d9b175455690a552555fa76ab4211638248c5b"
127.0.0.1:6379> script exists f3d9b175455690a552555fa76ab4211638248c5b
1) (integer) 1
127.0.0.1:6379> script flush 
OK
127.0.0.1:6379> script exists f3d9b175455690a552555fa76ab4211638248c5b
1) (integer) 0


6>evalsh执行一个lua脚本

127.0.0.1:6379> script load "return 'sunshine'"
"f3d9b175455690a552555fa76ab4211638248c5b"
127.0.0.1:6379> evalsha "f3d9b175455690a552555fa76ab4211638248c5b" 0
"sunshine"


四、连接


1>select 切换指定数据库,默认0-15

127.0.0.1:6379> select 0
OK
127.0.0.1:6379> select 5
OK
127.0.0.1:6379[5]> select 10
OK
127.0.0.1:6379[10]> select 15
OK
127.0.0.1:6379[15]> select 16
(error) ERR invalid DB index

2>quit关闭当前连接当然还有其他方法exit或者Crtl+c

127.0.0.1:6379> quit
[root@redis_master ~]# redis-server -a xxxx
6467:C 28 Sep 22:24:12.514 # Fatal error, can't open config file '-a'
[root@redis_master ~]# redis-cli -a xxxx
127.0.0.1:6379> exit
[root@redis_master ~]# redis-cli -a xxxx
127.0.0.1:6379> 
[root@redis_master ~]#

3>ping,测试是否redis是否存活

127.0.0.1:6379> ping
PONG
\\打开new session
[root@redis_master ~]# killall redis-server
[root@redis_master ~]# 
\\再次执行ping
127.0.0.1:6379> ping
Could not connect to Redis at 127.0.0.1:6379: Connection refused

4>echo更bash一样

127.0.0.1:6379> echo Sunshine
"Sunshine"
127.0.0.1:6379> echo SunshineBoy
"SunshineBoy"
127.0.0.1:6379> echo SunshineBoySFZ
"SunshineBoySFZ"


5>auth认证,在配置文件设定

[root@redis_master ~]# vim  /usr/local/redis3.0.4/etc/redis.conf 

################################## SECURITY ###################################
... \\省略介绍信息
#
requirepass PASSWORD        \\设定密码并保存退出重启redis-server

[root@redis_master ~]# redis-cli 
127.0.0.1:6379> kys *
(error) ERR unknown command 'kys'
127.0.0.1:6379> AUTH PASSWORD    \\你在配置文件设定的密码
OK
127.0.0.1:6379> keys *
1) "name"

五、HyperLogLog


1>pfadd指定一个元素到HyperLogLog

127.0.0.1:6379> pfadd pfkey a b c d e f g
(integer) 1
127.0.0.1:6379> pfcount pfkey
(integer) 7


2>pfcount显示返回给定HyperLogLog基数

127.0.0.1:6379> pfadd pfkey a b c d e f g
(integer) 1
127.0.0.1:6379> pfcount pfkey
(integer) 7
127.0.0.1:6379> pfadd pfkey a b c d e f g
(integer) 0
127.0.0.1:6379> pfadd pfkey a b c d e f g
(integer) 0
127.0.0.1:6379> pfcount pfkey
(integer) 7

3>pgmerge将多个HyperLogLog合并为一个

127.0.0.1:6379> pfadd pfkey3 aa bb cc dd 
(integer) 1
127.0.0.1:6379> pfadd pfkey4 a b c d
(integer) 1
127.0.0.1:6379> pfcount pfkey3
(integer) 4
127.0.0.1:6379> pfcount pfkey4
(integer) 4
127.0.0.1:6379> pfmerge pfkey5 pfkey3 pfkey4 
OK
127.0.0.1:6379> pfcount pfkey5
(integer) 8



至此Redis发布订阅、事务、脚本、连接、HyperLogLo就介绍完毕啦~明天或者后天就会谈到Redis服务器命令~ ^-^



欢迎大家加入 459479177QQ群进行交流