這篇blog主要總結下,redis中跟key相關的常用函式
1)keys
語法:keys pattern
解釋:查詢所有匹配指定模式pattern的key
[plain]view plain
copy
[root@xsf001 ~]# redis-cli
redis 127.0.0.1:6379> keys * #所有key
1) "score"
2) "stu"
3) "score1"
4) "dest"
5) "lst.user"
6) "lst.tect"
redis 127.0.0.1:6379> keys scor?
1) "score"
redis 127.0.0.1:6379> keys scor[ee1]
1) "score"
2)randomkey
語法:randomkey
解釋:返回乙個隨機key
[plain]view plain
copy
redis 127.0.0.1:6379> randomkey
"score"
redis 127.0.0.1:6379> randomkey
"list.user"
3)exists
語法:exists key
解釋:判斷乙個key是否存在
[plain]view plain
copy
redis 127.0.0.1:6379> exists score #key存在 返回1
(integer) 1
redis 127.0.0.1:6379> exists scorefda #key不存在 返回0
(integer) 0
4)type
語法:type key
解釋:返回key所儲存的值型別,返回值:none【key不存在】,string,list ,set, zset和hash
[plain]view plain
copy
redis 127.0.0.1:6379> type score
zset
redis 127.0.0.1:6379> type lst.user
list
5)expire
語法:expire key seconds
解釋:設定key的生存時間,單位是秒,當key過期時,會被自動刪除
[plain]view plain
copy
redis 127.0.0.1:6379> expire dest 30
(integer) 1
redis 127.0.0.1:6379> expire dest1 30 # key不存在
(integer) 0
6)ttl
語法: ttl key
解釋:得到key能存活時間,如果key不存在或沒有設定生存時間時,返回-1
[plain]view plain
copy
redis 127.0.0.1:6379> expire diff 100
(integer) 1
redis 127.0.0.1:6379> ttl diff
(integer) 94
redis 127.0.0.1:6379> ttl diff
(integer) 92
7)persist
語法:persist key
解釋:移除給定key的生存時間
[plain]view plain
copy
redis 127.0.0.1:6379> ttl diff
(integer) 28
redis 127.0.0.1:6379> persist diff
(integer) 1
redis 127.0.0.1:6379> ttl diff
(integer) -1
8)rename
語法:rename key newkey
解釋:將key改名為newkey
[plain]view plain
copy
redis 127.0.0.1:6379> smembers diff
1) "zhangsan01"
redis 127.0.0.1:6379> rename diff diff01
ok
redis 127.0.0.1:6379> smembers diff
(empty list or set)
redis 127.0.0.1:6379> smembers diff01
1) "zhangsan01"
redis 127.0.0.1:6379> rename diff diff01
(error) err no such key
注意:當key和newkey相同或key不存在時,返回錯誤;當newkey已存在時,rename將覆蓋舊值。
9)renamenx
語法:renamenx key newkey
解釋:當且僅當newkey不存在時,改名key
[plain]view plain
copy
redis 127.0.0.1:6379> renamenx diff01 stu #stu存在
(integer) 0
redis 127.0.0.1:6379> renamenx diff01 diff #diff不存在
(integer) 1
10)del
語法:del key [key ...]
解釋:刪除乙個或多個key
[plain]view plain
copy
redis 127.0.0.1:6379> del score1
(integer) 1
redis 127.0.0.1:6379> del union diff aa #key aa 不存在
(integer) 2
11)move
語法:move key db
解釋:將key移動到指定db
[plain]view plain
copy
redis 127.0.0.1:6379> smembers stu #預設0
1) "zhangsan01"
2) "wangwu"
redis 127.0.0.1:6379> move stu 1 #移動到 1
(integer) 1
redis 127.0.0.1:6379> smembers stu
(empty list or set)
redis 127.0.0.1:6379> select 1 #選擇db 1
ok
redis 127.0.0.1:6379[1]> smembers stu
1) "zhangsan01"
2) "wangwu"
12)sort
語法:sort key [by pattern] [limit offset count] [get pattern [get pattern ...]] [asc|desc] [alpha] [store destination]
解釋:返回或保持給定列表,集合,有序集合key中經過排序的元素
[plain]view plain
copy
redis 127.0.0.1:6379> sort lst.tech limit 0 2 alpha desc #按字符集排序
1) "tec06"
2) "tec05"
redis 127.0.0.1:6379> sort lst.stud
1) "1"
2) "2"
3) "3"
redis 127.0.0.1:6379> sort lst.stud desc
1) "3"
2) "2"
3) "1"
redis 127.0.0.1:6379> keys stu.*
1) "stu.name.2"
2) "stu.name.3"
3) "stu.level.1"
4) "stu.level.2"
5) "stu.level.3"
6) "stu.name.1"
redis 127.0.0.1:6379> sort lst.stud by stu.level.* desc get stu.level.* get stu.name.*
1) "3"
2) "admin"
3) "2"
4) "joe"
5) "1"
6) "jim"
redis 127.0.0.1:6379> sort lst.stud by stu.level.* asc get stu.name.*
1) "jim"
2) "joe"
3) "admin"
sort雖然很「厲害」,但盡量不要讓redis伺服器來sort大量的資料。可以通過設定閥值減少要sort的資料,或把排序操作向前移,在web伺服器或各個應用上來sort。
Redis筆記(四 KEY相關命令
keys 查詢所有符合給定模式pattern 正規表示式 的 key keys pattern 127.0 0.1 6379 keys 1 say 2 res 3 userinfo2 4 test7 5 test4 6 userinfo3 7 counter 8 test6 9 test 10 fo...
Docker 系列二 docker 映象相關操作
1.載乙個 ubuntu 的基礎映象 2.利用映象建立乙個容器,在其中執行 bash 應用 4.使用 tag 命令新增映象標籤,類似於鏈結的作用,標籤不同,id 不同,指向同一目標 docker tag ubuntu latest myubuntu latest 5.使用 docker inspec...
Redis系列 儲存篇hash主要操作函式小結
陽光透過玻璃,灑在身上,一杯暖茶在手,說不盡的安逸自得,讓我有種想再寫篇blog的衝動。上篇主要談了string,這裡談談hash吧 hash是一些列key value field value 的對映表。常常用其儲存一些物件例項。相對於把乙個物件的各個字段儲存為string,儲存為hash會占用更少...