1. 程式人生 > >Redis 的數據類型 - Keys 相關的命令

Redis 的數據類型 - Keys 相關的命令

ring ont 當前 edi ash expire 重命名 不存在 之前

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 相關的命令