06 redis 五大型別 list

2021-10-10 03:03:38 字數 4406 閱讀 5944

list

基本資料型別,列表

在redis裡面,可以把list玩成 堆,棧

###################################3

###基本命令

127.0.0.1:6379> lpush list one ###把乙個值,放在列表的頭部(l)

(integer) 1

127.0.0.1:6379> lpush list two

(integer) 2

127.0.0.1:6379> lpush list three

(integer) 3

127.0.0.1:6379> lrange list 0 -1

1)"three"

2)"two"

3)"one"

127.0.0.1:6379> rpush list rrr ###把乙個值,放在列表的尾部(r)

(integer) 4

127.0.0.1:6379> lrange list 0 1

1)"three"

2)"two"

127.0.0.1:6379> lrange list 0 -1

1)"three"

2)"two"

3)"one"

4)"rrr"

127.0.0.1:6379>

###################################3

lpop 移除第乙個值

rpop 移除最後乙個值

127.0.0.1:6379> lpop list

"three"

127.0.0.1:6379> lrange list 0 -1

1)"two"

2)"one"

3)"rrr"

127.0.0.1:6379> rpop list

"rrr"

127.0.0.1:6379> lrange list 0 -1

1)"two"

2)"one"

127.0.0.1:6379>

#################################3

lindex 獲取某乙個值

127.0.0.1:6379> lindex list 0

"two"

127.0.0.1:6379> lindex list 1

"one"

127.0.0.1:6379>

###################################3

llen 獲取列表的長度

127.0.0.1:6379> rpush list one

(integer) 1

127.0.0.1:6379> rpush list two

(integer) 2

127.0.0.1:6379> rpush list three

(integer) 3

127.0.0.1:6379> lrange list 0 -1

1)"one"

2)"two"

3)"three"

127.0.0.1:6379> llen list

(integer) 3

127.0.0.1:6379>

###################################3

移除指定的值 (查詢刪除)

lrem key count value

127.0.0.1:6379> lrange list 0 -1

1)"one"

2)"two"

3)"three"

4)"three"

127.0.0.1:6379> lrem list 1 one

(integer) 1

127.0.0.1:6379> lrange list 0 -1

1)"two"

2)"three"

3)"three"

127.0.0.1:6379> lrem list 1 three

(integer) 1

127.0.0.1:6379> lrange list 0 -1

1)"two"

2)"three"

127.0.0.1:6379> lpush list three

(integer) 3

127.0.0.1:6379> lrange list 0 -1

1)"three"

2)"two"

3)"three"

127.0.0.1:6379> lrem list 2 three

(integer) 2

127.0.0.1:6379>

###################################

ltrim 修剪列表

127.0.0.1:6379> rpush mylist hello

(integer) 1

127.0.0.1:6379> rpush mylist hello1

(integer) 2

127.0.0.1:6379> rpush mylist hello2

(integer) 3

127.0.0.1:6379> rpush mylist hello3

(integer) 4

127.0.0.1:6379> ltrim mylist 1 2 #通過下標,擷取指定的長度,這個list已經被改變了,只剩下擷取的元素

ok127.0.0.1:6379> lrange mylist 0 -1

1)"hello1"

2)"hello2"

127.0.0.1:6379>

###################################

rpoplpush #移除最後乙個元素,並加到另乙個列表中(移動)

127.0.0.1:6379> lrange mylist 0 -1

1)"hello1"

2)"hello2"

127.0.0.1:6379> rpoplpush mylist myotherlis

"hello2"

127.0.0.1:6379> lrange mylist 0 -1

1)"hello1"

127.0.0.1:6379> lrange myotherlis 0 -1

1)"hello2"

127.0.0.1:6379>

###################################

lset 指定下標,替換值(更新)

127.0.0.1:6379> exists list

(integer) 0

127.0.0.1:6379> lset list 0 item

(error) err no such key

127.0.0.1:6379> lpush list item

(integer) 1

127.0.0.1:6379> lrange list 0 0

1)"item"

127.0.0.1:6379> lset list 0 value2

ok127.0.0.1:6379> lrange list 0 0

1)"value2"

127.0.0.1:6379>

###################################

linsert 在指定的位置插入值(插入)

127.0.0.1:6379> rpush mylist hello

(integer) 1

127.0.0.1:6379> rpush mylist hello2

(integer) 2

127.0.0.1:6379> linsert mylist before hello2 other #插前面

(integer) 3

127.0.0.1:6379> lrange 0 -1

(error) err wrong number of arguments for

'lrange'

command

127.0.0.1:6379> lrange mylist 0 -1

1)"hello"

2)"other"

3)"hello2"

127.0.0.1:6379>

127.0.0.1:6379> linsert mylist after other 222 #插後面

(integer) 4

127.0.0.1:6379> lrange mylist 0 -1

1)"hello"

2)"other"

3)"222"

4)"hello2"

127.0.0.1:6379>

小結

用途,訊息排隊,訊息佇列 lpush rpop 棧lpush lpop

redis五大型別以及相關命令

連線雲伺服器後,輸入命令redis cli進入redis.string set u 5333 將字串u設定為5333 getrange u 0 1 獲取儲存在keyu中全部字串 incr u 將u增1 decr u 將u減1 incrby u 3 將u增加3 decrby u 3 將u減去3 inc...

Redis五大型別之String操作

檢視和連線redis命令列 redis cli help 檢視所有string型別的操作 help stringstring型別分為三種 1.string 2.number 3.bit 應用場景 秒殺 incr 分布式鎖setnx 一 string 型別 1 插入資料 set k1 aa 2 如果k...

05 redis五大型別string

string型別 字串 127.0.0.1 6379 set key1 v1 設定值 ok127.0.0.1 6379 get key1 取值 v1 127.0.0.1 6379 keys 檢視所有值 1 key1 127.0.0.1 6379 exists key1 key是否存在 integer...