1. 程式人生 > >redis基礎命令之key基本操作命令

redis基礎命令之key基本操作命令

redis是key-value儲存的,放在記憶體中,並在磁碟持久化的資料結構儲存系統,它可以用作資料庫、快取和訊息中介軟體。

通過set key value來儲存,通過get key來獲取值

一.建立key----set/setnx/mset/msetnx

1.建立單個key

set  key value:設定key的值,若存在則覆蓋

setnx key value:設定key的值,若存在則不操作

192.168.10.12:6379> set gw1 10

OK

192.168.10.12:6379> get gw1

"10"

192.168.10.12:6379> keys *

1) "gw1"

192.168.10.12:6379> set gw2 20

OK

192.168.10.12:6379> get gw2

"20"

192.168.10.12:6379> keys *

1) "gw1"

2) "gw2"

192.168.10.12:6379> setnx gw4 40

(integer) 1

192.168.10.12:6379> get gw4

"40"

192.168.10.12:6379> setnx gw4 50

(integer) 0

192.168.10.12:6379> get gw4

"40"

2.建立多個key

MSET key1 value1 key2 value2 ... keyN valueN   :設定這些key的值,若存在則覆蓋

MSETNX key1 value1 key2 value2 ... keyN valueN:同mset,但如果其中一個key已經存在了,則都不設定。這些操作都是原子的

192.168.10.12:6379> mset g1 a g2 b g3 c

OK

192.168.10.12:6379> keys *

4) "g2"

7) "g1"

8) "g3"

192.168.10.12:6379> mset g3 d g4 f g5 g

OK

192.168.10.12:6379> keys *

5) "g4"

9) "g3"

11) "g5"

192.168.10.12:6379> get g3

"d"

192.168.10.12:6379> get g4

"f"

192.168.10.12:6379> get g5

"g"

192.168.10.12:6379> msetnx g5 h g6 i g7k

(integer) 0

192.168.10.12:6379> keys *

5) "g4"

9) "g3"

11) "g5"

192.168.10.12:6379> get g5

"g"

192.168.10.12:6379> get g6

(nil)

二.檢視key

1.檢視所有key----keys *

keys *

keys "*"

2.根據key檢視value----getkey

192.168.10.12:6379> set gw1 10

OK

192.168.10.12:6379> get gw1

"10"

192.168.10.12:6379> keys *

1) "gw1"

192.168.10.12:6379> set gw2 20

OK

192.168.10.12:6379> get gw2

"20"

192.168.10.12:6379> keys *

1) "gw1"

2) "gw2"

3.檢視匹配字首或字尾的key----keys *substr*

192.168.10.12:6379> keys *

1) "gw1"

2) "gw2"

3) "gao2"

4) "gao1"

192.168.10.12:6379> keys gw*

1) "gw1"

2) "gw2"

192.168.10.12:6379> keys gao*

1) "gao2"

2) "gao1"

192.168.10.12:6379> keys *g*

1) "gw1"

2) "gw2"

3) "gao2"

4) "gao1"

192.168.10.12:6379> keys *1

1) "gw1"

2) "gao1"

4.隨機取出一個key----randomkey

192.168.10.12:6379> randomkey

"gao2"

192.168.10.12:6379> randomkey

"gao2"

192.168.10.12:6379> randomkey

"gw2"

192.168.10.12:6379> randomkey

"gw2"

192.168.10.12:6379> randomkey

"gao2"

5.檢視key的型別----typekey key

192.168.10.12:6379> type gw1

string

192.168.10.12:6379> type gao1

string

6.檢視當前資料庫中key的數量----dbsize

192.168.10.12:6379> keys *

1) "gw1"

2) "gw2"

3) "gao2"

4) "gao1"

192.168.10.12:6379> dbsize

(integer) 4

192.168.10.12:6379> select 1

OK

192.168.10.12:6379[1]> dbsize

(integer) 0

192.168.10.12:6379[1]> select 0

OK

192.168.10.12:6379> dbsize

(integer) 4

7.檢視某一個key是否存在----exists key

192.168.10.12:6379> exists gao1

(integer) 1

192.168.10.12:6379> exists ggao1

(integer) 0

三.刪除key----del key

192.168.10.12:6379> del gao1

(integer) 1

192.168.10.12:6379> get gao1

(nil)

192.168.10.12:6379> del gao1

(integer) 0

四.重新命名key----rename old new

192.168.10.12:6379> get gw1

"10"

192.168.10.12:6379> rename gw1 ggw1

OK

192.168.10.12:6379> get gw1

(nil)

192.168.10.12:6379> get ggw1

"10"

五.向key的字串追加拼接----append key value

192.168.10.12:6379> get gao1

"a"

192.168.10.12:6379> append gao1 b

(integer) 2

192.168.10.12:6379> get gao1

"ab"

六.獲取並重設某個key----getset key value

獲取該元素的值,並給該元素設定新值。

通常和incr搭配使用,比如一個mycount一直incr,然後達到某些情況需要清零,清零之前需要知道mycount的值

192.168.10.12:6379> get gao1

"ab"

192.168.10.12:6379> getset gao1 cd

"ab"

192.168.10.12:6379> get gao1

"cd"