记录一些常用的Redis命令和例子。
Key (键)
DEL 删除给定的一个或多个 key 。
1 2 3 4 5
| redis> SET name huangz OK
redis> DEL name (integer) 1
|
DUMP 序列化给定 key ,并返回被序列化的值
使用 RESTORE 命令可以将这个值反序列化为 Redis 键。
1 2 3 4 5 6 7 8
| redis> SET greeting "hello, dumping world!" OK
redis> DUMP greeting "\x00\x15hello, dumping world!\x06\x00E\xa0Z\x82\xd8r\xc1\xde"
redis> DUMP not-exists-key (nil)
|
EXISTS 检查给定 key 是否存在。
1 2 3 4 5 6 7 8 9 10 11
| redis> SET db "redis" OK
redis> EXISTS db (integer) 1
redis> DEL db (integer) 1
redis> EXISTS db (integer) 0
|
EXPIRE 为给定 key 设置生存时间
当 key 过期时(生存时间为 0 ),它会被自动删除。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| redis> SET cache_page "www.google.com" OK
redis> EXPIRE cache_page 30 (integer) 1
redis> TTL cache_page (integer) 23
redis> EXPIRE cache_page 30000 (integer) 1
redis> TTL cache_page (integer) 29996
|
EXPIREAT 用于为 key 设置生存时间
1 2 3 4 5 6 7 8
| redis> SET cache www.google.com OK
redis> EXPIREAT cache 1355292000 (integer) 1
redis> TTL cache (integer) 45081860
|
不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)
KEYS 查找所有符合给定模式 pattern 的 key 。
KEYS * 匹配数据库中所有 key 。
KEYS h?llo 匹配 hello , hallo 和 hxllo 等。
KEYS h * llo 匹配 hllo 和 heeeeello 等。
KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| redis> MSET one 1 two 2 three 3 four 4 OK
redis> KEYS *o* 1) "four" 2) "two" 3) "one"
redis> KEYS t?? 1) "two"
redis> KEYS t[w]* 1) "two"
redis> KEYS * 1) "four" 2) "three" 3) "two"
|
MIGRATE 将 key 原子性地从当前实例传送到目标实例的指定数据库上
一旦传送成功, key 保证会出现在目标实例上,而当前实例上的 key 会被删除。
这个命令是一个原子操作,它在执行的时候会阻塞进行迁移的两个实例,直到以下任意结果发生:迁移成功
,迁移失败
,等待超时
。
1 2 3 4 5 6 7 8 9 10 11 12 13
| $ ./redis-cli
redis 127.0.0.1:6379> flushdb OK
redis 127.0.0.1:6379> SET greeting "Hello from 6379 instance" OK
redis 127.0.0.1:6379> MIGRATE 127.0.0.1 7777 greeting 0 1000 OK
redis 127.0.0.1:6379> EXISTS greeting (integer) 0
|
MOVE 将当前数据库的 key 移动到给定的数据库 db 当中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
redis> SELECT 0 OK
redis> SET song "secret base - Zone" OK
redis> MOVE song 1 (integer) 1
redis> EXISTS song (integer) 0
redis> SELECT 1 OK
redis:1> EXISTS song (integer) 1
|
OBJECT 命令允许从内部察看给定 key 的 Redis 对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| redis> SET game "COD" OK
redis> OBJECT REFCOUNT game (integer) 1
redis> OBJECT IDLETIME game (integer) 90
redis> GET game "COD"
redis> OBJECT IDLETIME game (integer) 0
redis> OBJECT ENCODING game "raw"
redis> SET big-number 23102930128301091820391092019203810281029831092 OK
redis> OBJECT ENCODING big-number "raw"
redis> SET small-number 12345 OK
redis> OBJECT ENCODING small-number "int"
|
PERSIST 移除给定 key 的生存时间
将这个 key 从『易失的』(带生存时间 key )转换成『持久的』(一个不带生存时间、永不过期的 key )
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| redis> SET mykey "Hello" OK
redis> EXPIRE mykey 10 (integer) 1
redis> TTL mykey (integer) 10
redis> PERSIST mykey (integer) 1
redis> TTL mykey (integer) -1
|
PEXPIRE 以毫秒为单位设置 key 的生存时间,而不像 EXPIRE 命令那样,以秒为单位。
1 2 3 4 5 6 7 8 9 10 11
| redis> SET mykey "Hello" OK
redis> PEXPIRE mykey 1500 (integer) 1
redis> TTL mykey (integer) 2
redis> PTTL mykey (integer) 1499
|
PEXPIREAT 以毫秒为单位设置 key 的过期 unix 时间戳,而不是像 EXPIREAT 那样,以秒为单位。
这个命令和 EXPIREAT 命令类似
1 2 3 4 5 6 7 8 9 10 11
| redis> SET mykey "Hello" OK
redis> PEXPIREAT mykey 1555555555005 (integer) 1
redis> TTL mykey (integer) 223157079
redis> PTTL mykey (integer) 223157079318
|
PTTL 以毫秒为单位返回 key 的剩余生存时间,而不是像 TTL 命令那样,以秒为单位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
redis> FLUSHDB OK
redis> PTTL key (integer) -2
redis> SET key value OK
redis> PTTL key (integer) -1
redis> PEXPIRE key 10086 (integer) 1
redis> PTTL key (integer) 6179
|
RANDOMKEY 从当前数据库中随机返回(不删除)一个 key 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
redis> MSET fruit "apple" drink "beer" food "cookies" OK
redis> RANDOMKEY "fruit"
redis> RANDOMKEY "food"
redis> KEYS * 1) "food" 2) "drink" 3) "fruit"
redis> FLUSHDB OK
redis> RANDOMKEY (nil)
|
RENAME 将 key 改名为 newkey 。
当 key 和 newkey 相同,或者 key 不存在时,返回一个错误。
当 newkey 已经存在时, RENAME 命令将覆盖旧值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
redis> SET message "hello world" OK
redis> RENAME message greeting OK
redis> EXISTS message (integer) 0
redis> EXISTS greeting (integer) 1
redis> RENAME fake_key never_exists (error) ERR no such key
redis> SET pc "lenovo" OK
redis> SET personal_computer "dell" OK
redis> RENAME pc personal_computer OK
redis> GET pc (nil)
redis:1> GET personal_computer "lenovo"
|
RENAMENX 当且仅当 newkey 不存在时,将 key 改名为 newkey 。
当 key 不存在时,返回一个错误。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
redis> SET player "MPlyaer" OK
redis> EXISTS best_player (integer) 0
redis> RENAMENX player best_player (integer) 1
redis> SET animal "bear" OK
redis> SET favorite_animal "butterfly" OK
redis> RENAMENX animal favorite_animal (integer) 0
redis> get animal "bear"
redis> get favorite_animal "butterfly"
|
RESTOR 反序列化给定的序列化值,并将它和给定的 key 关联。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
redis> SET greeting "hello, dumping world!" OK
redis> DUMP greeting "\x00\x15hello, dumping world!\x06\x00E\xa0Z\x82\xd8r\xc1\xde"
redis> RESTORE greeting-again 0 "\x00\x15hello, dumping world!\x06\x00E\xa0Z\x82\xd8r\xc1\xde" OK
redis> GET greeting-again "hello, dumping world!"
redis> RESTORE greeting-again 0 "\x00\x15hello, dumping world!\x06\x00E\xa0Z\x82\xd8r\xc1\xde" (error) ERR Target key name is busy.
redis> RESTORE greeting-again 0 "\x00\x15hello, dumping world!\x06\x00E\xa0Z\x82\xd8r\xc1\xde" REPLACE OK
redis> RESTORE fake-message 0 "hello moto moto blah blah" (error) ERR DUMP payload version or checksum are wrong
|
SORT 返回或保存给定列表、集合、有序集合 key 中经过排序的元素。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
redis> LPUSH today_cost 30 1.5 10 8 (integer) 4
redis> SORT today_cost 1) "1.5" 2) "8" 3) "10" 4) "30"
redis 127.0.0.1:6379> SORT today_cost DESC 1) "30" 2) "10" 3) "8" 4) "1.5"
|
TTL 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
redis> FLUSHDB OK
redis> TTL key (integer) -2
redis> SET key value OK
redis> TTL key (integer) -1
redis> EXPIRE key 10086 (integer) 1
redis> TTL key (integer) 10084
|
TYPE 返回 key 所储存的值的类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
redis> SET weather "sunny" OK
redis> TYPE weather string
redis> LPUSH book_list "programming in scala" (integer) 1
redis> TYPE book_list list
redis> SADD pat "dog" (integer) 1
redis> TYPE pat set
|
SCAN 命令及其相关的 SSCAN 命令、 HSCAN 命令和 ZSCAN 命令都用于增量地迭代
- SCAN 命令用于迭代当前数据库中的数据库键。
- SSCAN 命令用于迭代集合键中的元素。
- HSCAN 命令用于迭代哈希键中的键值对。
- ZSCAN 命令用于迭代有序集合中的元素(包括元素成员和元素分值)。
String (字符串)
APPEND 将 value 追加到 key 原来的值的末尾
如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。
如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
redis> EXISTS myphone (integer) 0
redis> APPEND myphone "nokia" (integer) 5
redis> APPEND myphone " - 1110" (integer) 12
redis> GET myphone "nokia - 1110"
|
BITCOUNT 计算给定字符串中,被设置为 1 的比特位的数量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| redis> BITCOUNT bits (integer) 0
redis> SETBIT bits 0 1 (integer) 0
redis> BITCOUNT bits (integer) 1
redis> SETBIT bits 3 1 (integer) 0
redis> BITCOUNT bits (integer) 2
|
BITOP 对一个或多个保存二进制位的字符串 key 进行位元操作,并将结果保存到 destkey 上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| redis> SETBIT bits-1 0 1 (integer) 0
redis> SETBIT bits-1 3 1 (integer) 0
redis> SETBIT bits-2 0 1 (integer) 0
redis> SETBIT bits-2 1 1 (integer) 0
redis> SETBIT bits-2 3 1 (integer) 0
redis> BITOP AND and-result bits-1 bits-2 (integer) 1
redis> GETBIT and-result 0 (integer) 1
redis> GETBIT and-result 1 (integer) 0
redis> GETBIT and-result 2 (integer) 0
redis> GETBIT and-result 3 (integer) 1
|
DECR 将 key 中储存的数字值减一。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
redis> SET failure_times 10 OK
redis> DECR failure_times (integer) 9
redis> EXISTS count (integer) 0
redis> DECR count (integer) -1
redis> SET company YOUR_CODE_SUCKS.LLC OK
redis> DECR company (error) ERR value is not an integer or out of range
|
DECRBY 将 key 所储存的值减去减量 decrement 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
redis> SET count 100 OK
redis> DECRBY count 20 (integer) 80
redis> EXISTS pages (integer) 0
redis> DECRBY pages 10 (integer) -10
|
GET 返回 key 所关联的字符串值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
redis> GET db (nil)
redis> SET db redis OK
redis> GET db "redis"
redis> DEL db (integer) 1
redis> LPUSH db redis mongodb mysql (integer) 3
redis> GET db (error) ERR Operation against a key holding the wrong kind of value
|
GETBIT 对 key 所储存的字符串值,获取指定偏移量上的位(bit)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
redis> EXISTS bit (integer) 0
redis> GETBIT bit 10086 (integer) 0
redis> SETBIT bit 10086 1 (integer) 0
redis> GETBIT bit 10086 (integer) 1
|
GETRANGE 返回 key 中字符串值的子字符串
字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| redis> SET greeting "hello, my friend" OK
redis> GETRANGE greeting 0 4 "hello"
redis> GETRANGE greeting -1 -5 ""
redis> GETRANGE greeting -3 -1 "end"
redis> GETRANGE greeting 0 -1 "hello, my friend"
redis> GETRANGE greeting 0 1008611 "hello, my friend"
|
GETSET 将给定 key 的值设为 value ,并返回 key 的旧值(old value)。
当 key 存在但不是字符串类型时,返回一个错误。
1 2 3 4 5 6 7 8 9 10 11
| redis> GETSET db mongodb (nil)
redis> GET db "mongodb"
redis> GETSET db redis "mongodb"
redis> GET db "redis"
|
INCR 将 key 中储存的数字值增一。
1 2 3 4 5 6 7 8
| redis> SET page_view 20 OK
redis> INCR page_view (integer) 21
redis> GET page_view "21"
|
INCRBY 将 key 所储存的值加上增量 increment 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
redis> SET rank 50 OK
redis> INCRBY rank 20 (integer) 70
redis> GET rank "70"
redis> EXISTS counter (integer) 0
redis> INCRBY counter 30 (integer) 30
redis> GET counter "30"
redis> SET book "long long ago..." OK
redis> INCRBY book 200
|
INCRBYFLOAT 为 key 中所储存的值加上浮点数增量 increment 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
redis> SET mykey 10.50 OK
redis> INCRBYFLOAT mykey 0.1 "10.6"
redis> SET mykey 314e-2 OK
redis> GET mykey "314e-2"
redis> INCRBYFLOAT mykey 0 "3.14"
redis> SET mykey 3 OK
redis> INCRBYFLOAT mykey 1.1 "4.1"
redis> SET mykey 3.0 OK
redis> GET mykey "3.0"
redis> INCRBYFLOAT mykey 1.000000000000000000000 "4"
redis> GET mykey "4"
|
MGET 返回所有(一个或多个)给定 key 的值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| redis> SET redis redis.com OK
redis> SET mongodb mongodb.org OK
redis> MGET redis mongodb 1) "redis.com" 2) "mongodb.org"
redis> MGET redis mongodb mysql 1) "redis.com" 2) "mongodb.org" 3) (nil)
|
MSET 同时设置一个或多个 key-value 对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| redis> MSET date "2012.3.30" time "11:00 a.m." weather "sunny" OK
redis> MGET date time weather 1) "2012.3.30" 2) "11:00 a.m." 3) "sunny"
redis> SET google "google.hk" OK
redis> MSET google "google.com" OK
redis> GET google "google.com"
|
MSETNX 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
redis> MSETNX rmdbs "MySQL" nosql "MongoDB" key-value-store "redis" (integer) 1
redis> MGET rmdbs nosql key-value-store 1) "MySQL" 2) "MongoDB" 3) "redis"
redis> MSETNX rmdbs "Sqlite" language "python" (integer) 0
redis> EXISTS language (integer) 0
redis> GET rmdbs "MySQL"
|
SET 将字符串值 value 关联到 key 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
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"
|
Hash (哈希表)
List (列表)
Set (集合)
SortedSet (有序集合)
HyperLogLog
GEO (地理位置)
Pub/Sub (发布、订阅)
Transaction (事务)
Script (脚本)
Connection (连接)
Server (服务器)
参考