首页 > 代码库 > Redis 的数据类型 - Keys 相关的命令

Redis 的数据类型 - Keys 相关的命令

KEYS:返回所有符合给定模式的 key

  语法:KEYS pattern

    *:匹配任意个字符

    ?:匹配一个任意字符

    []:匹配[]之间的一个字符,[b-e],a[b-e] ab ac ad ae

    \x:匹配特殊字符\? \*

  MSET one ‘one‘ two ‘two‘ three ‘three‘ four ‘four‘ five ‘five‘ six ‘six‘ seven ‘seven‘

  KEYS *

  KEYS *o*

  KEYS t???

  KEYS ?s*

  KEYS c[n-z]*   #c开头,后面接 n-z 中任意一个字符#

127.0.0.1:6379> MSET one ‘one‘ two ‘two‘ three ‘three‘ four ‘four‘ five ‘five‘ six ‘six‘ seven ‘seven‘
OK

127.0.0.1:6379> KEYS *
 1) "test1"
 2) "myKey"
 3) "count"
 4) "test3"
 5) "three"
 6) "userInfo3"
 7) "count3"
 8) "test14"
 9) "test2"
10) "testHash1"
11) "test4"
12) "one"
13) "two"
14) "testStr1"
15) "count1"
16) "testStr3"
17) "test15"
18) "five"
19) "test9"
20) "testStr5"
21) "test"
22) "test10"
23) "testStr2"
24) "test13"
25) "testStr4"
26) "four"
27) "userInfo2"
28) "seven"
29) "count2"
30) "six"
31) "test6"
32) "test5"
33) "test8"

127.0.0.1:6379> KEYS *o*
1) "count"
2) "userInfo3"
3) "count3"
4) "one"
5) "two"
6) "count1"
7) "four"
8) "userInfo2"
9) "count2"

127.0.0.1:6379> KEYS t???
1) "test"

127.0.0.1:6379> KEYS ?s*
1) "userInfo3"
2) "userInfo2"

127.0.0.1:6379> KEYS c[n-z]*
1) "count"
2) "count3"
3) "count1"
4) "count2"

 

EXISTS:检测指定key是否存在

  语法:EXISTS key

    EXISTS one

127.0.0.1:6379> EXISTS one
(integer) 1

 

TYPE:返回key所存储的类型

  语法:TYPE key

    TYPE test1

    TYPE userInfo2  #不存在的key 返回 none,存在返回 key的 类型 (string, hash, set, zset, list) #

127.0.0.1:6379> TYPE test1
string

127.0.0.1:6379> TYPE userInfo2
hash

127.0.0.1:6379> TYPE userInfo22
none

 

EXPIRE:设置 key 的过期时间

  语法:EXPIRE key seconds

    SET cache_page ‘http://www.cnblogs.com/shuo-128/‘

    EXPIRE cache_page 100

    TTL cache_page  #如果 key 已经存在过期时间,在通过 EXPIRE 设置的时候会覆盖之前过期时间#

127.0.0.1:6379> SET cache_page ‘http://www.cnblogs.com/shuo-128/‘
OK

127.0.0.1:6379> EXPIRE cache_page 100
(integer) 1

127.0.0.1:6379> TTL cache_page
(integer) 94

127.0.0.1:6379> TTL cache_page
(integer) 92

127.0.0.1:6379> TTL cache_page
(integer) 85

 

EXPIREAT:需要指定在指定时间戳过期

  语法:EXPIREAT key timestamp  #这里设置过期时间要是时间戳,现在时间为 1498364329 的话 设置 10000 毫秒后过期要在时间戳上加 10000#

    SET cache_page1 ‘http://www.redis.com‘

    EXPIREAT cache_page1 1498374329

127.0.0.1:6379> SET cache_page1 ‘http://www.redis.com‘
OK

127.0.0.1:6379> EXPIREAT cache_page1 1498374329
(integer) 1

 

PEXPIRE:以毫秒的形式指定过期时间

  语法:PEXIRE key milliseconds

    SET cache_page2 ‘http://www.redis.com‘

    PEXPIRE cache_page2 80000

    PTTL cache_page2

127.0.0.1:6379> SET cache_page2 ‘http://www.redis.com‘
OK

127.0.0.1:6379> PEXPIRE cache_page2 80000
(integer) 1

127.0.0.1:6379> TTL cache_page2
(integer) 66

127.0.0.1:6379>
127.0.0.1:6379> TTL cache_page2
(integer) 50

127.0.0.1:6379> TTL cache_page2
(integer) 41

127.0.0.1:6379> PTTL cache_page2
(integer) 24031

 

PEXPIREAT:指定时间戳,单位为毫秒

  语法:PEXPIREAT key timestamp

    SET cache_page3 ‘http://www.cnblogs.com/shuo-128/‘

    PEXPIREAT cache_page3 149837432910000000

    PTTL cache_page3

127.0.0.1:6379> PEXPIREAT cache_page3 149837432910000000
(integer) 1

127.0.0.1:6379> PTTL cache_page3
(integer) 149835934535432563

127.0.0.1:6379> PTTL cache_page3
(integer) 149835934535427739

 

TTL:以秒为单位返回 key 剩余时间

  语法:TTL key

    SET cache_page4 ‘http://www.baidu.com‘

    TTL cache_page4

    TTL cache_page5

    EXPIRE cache_page4 100

    TTL cache_page4  #如果没有 key 没有设置过期时间,返回 -1,如果 key 不存在返回 -2 #

127.0.0.1:6379> SET cache_page4 ‘http://www.baidu.com‘
OK

127.0.0.1:6379> TTL cache_page4
(integer) -1

127.0.0.1:6379> TTL cache_page5
(integer) -2

127.0.0.1:6379> EXPIRE cache_page4 100
(integer) 1

127.0.0.1:6379> TTL cache_page4
(integer) 95

 

PTTL:以毫秒为单位返回 key 的剩余时间

  语法:PTTL key

    PTTL cache_page3

127.0.0.1:6379> PTTL cache_page3
(integer) 149835934535432563

 

PERSIST:将一个带有过期时间的 key 转变成永久的 key

  语法:PERSIST key

    PTTL cache_page3

    PERSIST cache_page3

    PTTL cache_page3

127.0.0.1:6379> PTTL cache_page3
(integer) 149835934535432563

127.0.0.1:6379> PERSIST cache_page3
(integer) 1

127.0.0.1:6379>PTTL cache_page3
(integer) -1

 

DEL:删除指定的 key

  语法:DEL key ...

    DEL one two three four

127.0.0.1:6379> DEL one two three four
(integer) 4

 

RANDOMKEY:随机的从当前数据库中返回一个 key

  语法:RANDOMKEY

    RANDOMKEY

127.0.0.1:6379> RANDOMKEY
"testStr3"

127.0.0.1:6379> RANDOMKEY
"testStr1"

 

RENAME:重名名一个键

  语法:RENAME key newkey

    SET testRename1 ‘rename1‘

    RENAME testRename1 testRename2

    GET testRename1

    RENAME testRename2 testRename2

    GET testRename2

    RENAME testRename2 test14  #如果名称没有发生改变会报错#

127.0.0.1:6379> SET testRename1 ‘rename1‘
OK

127.0.0.1:6379> RENAME testRename1 testRename2
OK

127.0.0.1:6379> RENAME testRename2 testRename2
OK

127.0.0.1:6379> GET testRename1
(nil)

127.0.0.1:6379> GET testRename2
"rename1"

 

RENAMENX:必须重命名这个新名称不存在才会生效

  语法:RENAMENX key newkey

    RENAMENX testRename3 tree

    RENAMENX testRename2 two

127.0.0.1:6379> RENAMENX testRename3 tree
(error) ERR no such key

127.0.0.1:6379> RENAMENX testRename2 two
(integer) 1

 

DUMP:序列化给定的 Key,返回序列化之后的值

  语法:DUMP key

    SET testDump ‘this is a Dump‘

    DUMP testDump

127.0.0.1:6379> SET testDump ‘this is a Dump‘
OK

127.0.0.1:6379> DUMP testDump
"\x00\x0ethis is a Dump\a\x00\x01\x80\xf5:\x0eI\xdb,"

 

RESTORE:反序列化

  语法:RESTORE key cache value 过期时间cache 为毫秒,不设置过期时间则为 0

    RESTORE testDump1 0 "\x00\x0ethis is a Dump\a\x00\x01\x80\xf5:\x0eI\xdb,"

127.0.0.1:6379> RESTORE testDump1 0 "\x00\x0ethis is a Dump\a\x00\x01\x80\xf5:\x0eI\xdb,"
OK

127.0.0.1:6379> GET testDump1
"this is a Dump"

 

MOVE:将当前数据库中的 key 移动到另外的数据库中

  语法:MOVE key dbId

    SELECT 0

    SET testMove ‘Move‘

    MOVE testMove 1

    SELECT 1

    MOVE testMove1 1  #当移动一个不存在的 key 会失败,当目录数据库中存在同名 key 的时候移动失败#

127.0.0.1:6379> SELECT 0
OK

127.0.0.1:6379> SET testMove ‘Move‘
OK

127.0.0.1:6379> MOVE testMove 1
(integer) 1

127.0.0.1:6379> SELECT 1
OK

127.0.0.1:6379[1]> KEYS *
1) "testMove"

127.0.0.1:6379[1]> GET testMove
"Move"

127.0.0.1:6379[1]> SELECT 0
OK

127.0.0.1:6379> SET testMove ‘Move‘
OK

127.0.0.1:6379> MOVE testMove 1
(integer) 0

127.0.0.1:6379> MOVE testMove1 1
(integer) 0

 

Redis 的数据类型 - Keys 相关的命令