天天看点

【Redis学习】Redis管理命令总结

1、键管理

之前通过对五种数据类型的操作命令的学习发现,Redis在对每种数据进行处理之前,都要先指定该数据的key,然后再指定对该数据进行何种操作。

Redis中的key有点类似于Java中的变量名。对某个数据的处理都是以key作为切入点。所以Redis把key作为单独的处理对象抽象出了一套操作命令。key可以想象成一个指向实际数据的指针,对key的操作会直接影响它所指向的数据的状态。

:> SET name tom
OK
:> GET name
"tom"
:> DEL name
(integer) 
:> GET name
(nil)
           

想要查看某个数据的类型:

:> SADD direction east west south north
(integer) 
:> TYPE direction
set
想要更改某个数据的key的名字:
:> RENAME direction direct
OK
:> SMEMBERS direct
) "north"
) "west"
) "south"
) "east"
           

1.1 key(键)命令

命令示例:

DEL key [key …]

删除给定的一个或多个 key 。不存在的 key 会被忽略。

返回值:被删除key的数量。

示例:

//删除单个 key

redis> SET name huangz

OK

redis> DEL name

(integer) 1

//删除一个不存在的 key

redis> EXISTS phone

(integer) 0

redis> DEL phone # 失败,没有 key 被删除

(integer) 0

//同时删除多个 key

redis> SET name “redis”

OK

redis> SET type “key-value store”

OK

redis> SET website “redis.com”

OK

redis> DEL name type website

(integer) 3

1.2 String(字符串)命令

命令示例:

SET key value [EX seconds] [PX milliseconds] [NX|XX]

将字符串值 value 关联到 key 。如果 key 已经持有其他值, SET 就覆写旧值,无视类型。

对于某个原本带有生存时间(TTL)的键来说, 当 SET 命令成功在这个键上执行时, 这个键原有的 TTL 将被清除。

可选参数:

从 Redis 2.6.12 版本开始, SET 命令的行为可以通过一系列参数来修改:

EX second :设置键的过期时间为 second 秒。 SET key value EX second 效果等同于 SETEX key second value 。

PX millisecond :设置键的过期时间为 millisecond 毫秒。 SET key value PX millisecond 效果等同于 PSETEX key millisecond value 。

NX :只在键不存在时,才对键进行设置操作。 SET key value NX 效果等同于 SETNX key value 。

XX :只在键已经存在时,才对键进行设置操作。

因为 SET 命令可以通过参数来实现和 SETNX 、 SETEX 和 PSETEX 三个命令的效果,所以将来的 Redis 版本可能会废弃并最终移除 SETNX 、 SETEX 和 PSETEX 这三个命令。

返回值:在 Redis 2.6.12 版本以前, SET 命令总是返回 OK 。

从 Redis 2.6.12 版本开始, SET 在设置操作成功完成时,才返回 OK 。

如果设置了 NX 或者 XX ,但因为条件没达到而造成设置操作未执行,那么命令返回空批量回复(NULL Bulk Reply)。

//对不存在的键进行设置

示例:

redis 127.0.0.1:6379> SET key “value”

OK

redis 127.0.0.1:6379> GET key

“value”

// 对已存在的键进行设置

redis 127.0.0.1:6379> SET key “new-value”

OK

redis 127.0.0.1:6379> GET key

“new-value”

//使用 EX 选项

redis 127.0.0.1:6379> SET key-with-expire-time “hello” EX 10086

OK

redis 127.0.0.1:6379> GET key-with-expire-time

“hello”

redis 127.0.0.1:6379> TTL key-with-expire-time

(integer) 10069

//使用 PX 选项

redis 127.0.0.1:6379> SET key-with-pexpire-time “moto” PX 123321

OK

redis 127.0.0.1:6379> GET key-with-pexpire-time

“moto”

redis 127.0.0.1:6379> PTTL key-with-pexpire-time

(integer) 111939

//使用 NX 选项

redis 127.0.0.1:6379> SET not-exists-key “value” NX

OK # 键不存在,设置成功

redis 127.0.0.1:6379> GET not-exists-key

“value”

redis 127.0.0.1:6379> SET not-exists-key “new-value” NX

(nil) # 键已经存在,设置失败

redis 127.0.0.1:6379> GEt not-exists-key

“value” # 维持原值不变

// 使用 XX 选项

redis 127.0.0.1:6379> EXISTS exists-key

(integer) 0

redis 127.0.0.1:6379> SET exists-key “value” XX

(nil) # 因为键不存在,设置失败

redis 127.0.0.1:6379> SET exists-key “value”

OK # 先给键设置一个值

redis 127.0.0.1:6379> SET exists-key “new-value” XX

OK # 设置新值成功

redis 127.0.0.1:6379> GET exists-key

“new-value”

// NX 或 XX 可以和 EX 或者 PX 组合使用

redis 127.0.0.1:6379> SET key-with-expire-and-NX “hello” EX 10086 NX

OK

redis 127.0.0.1:6379> GET key-with-expire-and-NX

“hello”

redis 127.0.0.1:6379> TTL key-with-expire-and-NX

(integer) 10063

redis 127.0.0.1:6379> SET key-with-pexpire-and-XX “old value”

OK

redis 127.0.0.1:6379> SET key-with-pexpire-and-XX “new value” PX 123321

OK

redis 127.0.0.1:6379> GET key-with-pexpire-and-XX

“new value”

redis 127.0.0.1:6379> PTTL key-with-pexpire-and-XX

(integer) 112999

// EX 和 PX 可以同时出现,但后面给出的选项会覆盖前面给出的选项

redis 127.0.0.1:6379> SET key “value” EX 1000 PX 5000000

OK

redis 127.0.0.1:6379> TTL key

(integer) 4993 # 这是 PX 参数设置的值

redis 127.0.0.1:6379> SET another-key “value” PX 5000000 EX 1000

OK

redis 127.0.0.1:6379> TTL another-key

(integer) 997 # 这是 EX 参数设置的值

1.3 hash(哈希表)

命令示例:

HSET key field value

将哈希表 key 中的域 field 的值设为 value 。如果 key 不存在,一个新的哈希表被创建并进行 HSET 操作。如果域 field 已经存在于哈希表中,旧值将被覆盖。

返回值:如果 field 是哈希表中的一个新建域,并且值设置成功,返回 1 。如果哈希表中域 field 已经存在且旧值已被新值覆盖,返回 0 。

redis> HSET website google “www.g.cn” # 设置一个新域

(integer) 1

redis> HSET website google “www.google.com” # 覆盖一个旧域

(integer) 0

1.4 List(列表)

命令示例:

LSET key index value

将列表 key 下标为 index 的元素的值设置为 value 。当 index 参数超出范围,或对一个空列表( key 不存在)进行 LSET 时,返回一个错误。

关于列表下标的更多信息,请参考 LINDEX 命令。

返回值:操作成功返回 ok ,否则返回错误信息。

//对空列表(key 不存在)进行 LSET

redis> EXISTS list

(integer) 0

redis> LSET list 0 item

(error) ERR no such key

//对非空列表进行 LSET

redis> LPUSH job “cook food”

(integer) 1

redis> LRANGE job 0 0

1) “cook food”

redis> LSET job 0 “play game”

OK

redis> LRANGE job 0 0

1) “play game”

// index 超出范围

redis> LLEN list # 列表长度为 1

(integer) 1

redis> LSET list 3 ‘out of range’

(error) ERR index out of range

1.5 Set(集合)

命令示例:

SADD key member [member …]

将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略。假如 key 不存在,则创建一个只包含 member 元素作成员的集合。当 key 不是集合类型时,返回一个错误。

在Redis2.4版本以前, SADD 只接受单个 member 值。

返回值:被添加到集合中的新元素的数量,不包括被忽略的元素。

// 添加单个元素

redis> SADD bbs “discuz.net”

(integer) 1

// 添加重复元素

redis> SADD bbs “discuz.net”

(integer) 0

// 添加多个元素

redis> SADD bbs “tianya.cn” “groups.google.com”

(integer) 2

redis> SMEMBERS bbs

1) “discuz.net”

2) “groups.google.com”

3) “tianya.cn”

1.6 SortedSet(有序集合)

命令示例:

ZADD key score member [[score member] [score member] …]

将一个或多个 member 元素及其 score 值加入到有序集 key 当中。如果某个 member 已经是有序集的成员,那么更新这个 member 的 score 值,并通过重新插入这个 member 元素,来保证该 member 在正确的位置上。score 值可以是整数值或双精度浮点数。如果 key 不存在,则创建一个空的有序集并执行 ZADD 操作。当 key 存在但不是有序集类型时,返回一个错误。

对有序集的更多介绍请参见 sorted set 。

在 Redis 2.4 版本以前, ZADD 每次只能添加一个元素。

返回值:被成功添加的新成员的数量,不包括那些被更新的、已经存在的成员。

// 添加单个元素

redis> ZADD page_rank 10 google.com

(integer) 1

// 添加多个元素

redis> ZADD page_rank 9 baidu.com 8 bing.com

(integer) 2

redis> ZRANGE page_rank 0 -1 WITHSCORES

1) “bing.com”

2) “8”

3) “baidu.com”

4) “9”

5) “google.com”

6) “10”

// 添加已存在元素,且 score 值不变

redis> ZADD page_rank 10 google.com

(integer) 0

redis> ZRANGE page_rank 0 -1 WITHSCORES # 没有改变

1) “bing.com”

2) “8”

3) “baidu.com”

4) “9”

5) “google.com”

6) “10”

// 添加已存在元素,但是改变 score 值

redis> ZADD page_rank 6 bing.com

(integer) 0

redis> ZRANGE page_rank 0 -1 WITHSCORES # bing.com 元素的 score 值被改变

1) “bing.com”

2) “6”

3) “baidu.com”

4) “9”

5) “google.com”

6) “10”

2、Pub/Sub发布订阅管理

发布订阅(pub/sub)是一种消息通信模式,主要的目的是解耦消息发布者和消息订阅者之间的耦合,这点和设计模式中的观察者模式比较相似。

Redis作为一个server,在订阅者和发布者之间起到了消息路由的功能。订阅者可以通过subscribe和psubscribe命令向Redis server订阅自己感兴趣的消息类型,Redis将消息类型称为通道(channel)。当发布者通过publish命令向Redis server发送特定类型的消息时。订阅该消息类型的全部client都会收到此消息。这里消息的传递是多对多的。一个client可以订阅多个 channel,也可以向多个channel发送消息。

例如,一个客户端订阅了“CCTV-5”频道的消息:

:> SUBSCRIBE CCTV-
Reading messages... (press Ctrl-C to quit)
) "subscribe"
) "CCTV-5"
) (integer) 
           

另一个客户端在“CCTV-5”发布了两条消息:

:> PUBLISH CCTV- "Kobe will say good bye to NBA in 2016.4.4"
(integer) 
:>PUBLISH CCTV- "Cavaliers Cleveland won the championship"
(integer) 
           

第一个客户端就会收到这两条消息:

:> SUBSCRIBE CCTV-
Reading messages... (press Ctrl-C to quit)
) "subscribe"
) "CCTV-5"
) (integer) 
)"message"
)"CCTV-5"
)"Kobe will say good bye to NBA in 2016.4.4"
)"message"
)"CCTV-5"
) "CavaliersCleveland won the championship"
           

命令示例:

PSUBSCRIBE pattern [pattern …]

订阅一个或多个符合给定模式的频道。每个模式以 * 作为匹配符,比如 it* 匹配所有以 it 开头的频道( it.news 、 it.blog 、 it.tweets 等等), news.* 匹配所有以 news. 开头的频道( news.it 、 news.global.today 等等),诸如此类。

返回值:接收到的信息(请参见下面的代码说明)。

// 订阅 news.* 和 tweet.* 两个模式

// 第 1 - 6 行是执行 psubscribe 之后的反馈信息

// 第 7 - 10 才是接收到的第一条信息

// 第 11 - 14 是第二条

// 以此类推。。。

redis> psubscribe news.* tweet.*

Reading messages… (press Ctrl-C to quit)

1) “psubscribe” # 返回值的类型:显示订阅成功

2) “news.*” # 订阅的模式

3) (integer) 1 # 目前已订阅的模式的数量

1) “psubscribe”

2) “tweet.*”

3) (integer) 2

1) “pmessage” # 返回值的类型:信息

2) “news.*” # 信息匹配的模式

3) “news.it” # 信息本身的目标频道

4) “Google buy Motorola” # 信息的内容

1) “pmessage”

2) “tweet.*”

3) “tweet.huangz”

4) “hello”

1) “pmessage”

2) “tweet.*”

3) “tweet.joe”

4) “@huangz morning”

1) “pmessage”

2) “news.*”

3) “news.life”

4) “An apple a day, keep doctors away”

3、Transaction事务管理

4、Script(脚本)管理命令

命令示例:

EVALSHA sha1 numkeys key [key …] arg [arg …]

根据给定的 sha1 校验码,对缓存在服务器中的脚本进行求值。将脚本缓存到服务器的操作可以通过 SCRIPT LOAD 命令进行。这个命令的其他地方,比如参数的传入方式,都和 EVAL 命令一样。

redis> SCRIPT LOAD “return ‘hello moto’”

“232fd51614574cf0867b83d384a5e898cfd24e5a”

redis> EVALSHA “232fd51614574cf0867b83d384a5e898cfd24e5a” 0

“hello moto”

5、connection连接管理命令

默认情况下,Redis没有密码要求,意味着无需通过密码验证就可以连接到Redis 服务。

可以通过更改配置文件中的“requirepass”配置项,来设置密码。

[email protected]:~$ Redis-cli
:> CONFIG get requirepass
) "requirepass"
) "" --默认没有密码
:> CONFIG set requirepass"chenlongfei"  --设置密码
OK
:> QUIT  --退出重新连接
[email protected]:~$ Redis-cli
:> SET name "clf"
(error) NOAUTH Authentication required.  –提示没权限
:> AUTH chenlongfei  --验证密码
OK
:> SET name "clf"  --之后才能进行操作
OK
:> GET name
"clf"
           

命令示例:

AUTH password

通过设置配置文件中 requirepass 项的值(使用命令 CONFIG SET requirepass password ),可以使用密码来保护 Redis 服务器。

如果开启了密码保护的话,在每次连接 Redis 服务器之后,就要使用 AUTH 命令解锁,解锁之后才能使用其他 Redis 命令。

如果 AUTH 命令给定的密码 password 和配置文件中的密码相符的话,服务器会返回 OK 并开始接受命令输入。

另一方面,假如密码不匹配的话,服务器将返回一个错误,并要求客户端需重新输入密码。

因为 Redis 高性能的特点,在很短时间内尝试猜测非常多个密码是有可能的,因此请确保使用的密码足够复杂和足够长,以免遭受密码猜测攻击。

返回值:密码匹配时返回 OK ,否则返回一个错误。

// 设置密码

redis> CONFIG SET requirepass secret_password # 将密码设置为 secret_password

OK

redis> QUIT # 退出再连接,让新密码对客户端生效

[[email protected]]$ redis

redis> PING # 未验证密码,操作被拒绝

(error) ERR operation not permitted

redis> AUTH wrong_password_testing # 尝试输入错误的密码

(error) ERR invalid password

redis> AUTH secret_password # 输入正确的密码

OK

redis> PING # 密码验证成功,可以正常操作命令了

PONG

//清空密码

redis> CONFIG SET requirepass “” # 通过将密码设为空字符来清空密码

OK

redis> QUIT

$ redis # 重新进入客户端

redis> PING # 执行命令不再需要密码,清空密码操作成功

PONG

6、Server(服务器)管理命令

Redis定义了一组与服务器相关的命令,用于查询服务器信息,如当前时间、客户端连接数量,以及修改配置文件、手动触发某些操作等。

命令示例:

SAVE

SAVE 命令执行一个同步保存操作,将当前 Redis 实例的所有数据快照(snapshot)以 RDB 文件的形式保存到硬盘。

一般来说,在生产环境很少执行 SAVE 操作,因为它会阻塞所有客户端,保存数据库的任务通常由 BGSAVE 命令异步地执行。然而,如果负责保存数据的后台子进程不幸出现问题时, SAVE 可以作为保存数据的最后手段来使用。

返回值:保存成功时返回 OK 。

redis> SAVE

OK

继续阅读