redis學習筆記二(List)

2021-10-10 03:07:54 字數 3157 閱讀 7625

lpush [key] [value],rpush [key] [value]

127.0.0.1:6379> lpush list one

(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"

127.0.0.1:6379> lrange list 0 2

1) "three"

2) "two"

3) "one"

127.0.0.1:6379> rpush list right

127.0.0.1:6379> lrange list 0 -1

1) "three"

2) "two"

3) "one"

4) "right"

lpop [keyname],rpop[keyname] #移除左/右元素

127.0.0.1:6379> lrange list 0 -1

1) "three"

2) "two"

3) "one"

4) "right"

127.0.0.1:6379> lpop list

"three"

127.0.0.1:6379> rpop list

"right"

127.0.0.1:6379> lrange list 0 -1

1) "two"

2) "one"

lindex [keyname] [index] #獲取指定位置的值

127.0.0.1:6379> lindex list 1

"one"

127.0.0.1:6379> lindex list 0

"two"

llen [keyname] #獲取list的長度

127.0.0.1:6379> llen list

(integer) 2

lrem [keyname] [count] [ele] #移除list中指定的值 count計數

127.0.0.1:6379> lrange list 0 -1

1) "two"

2) "one"

127.0.0.1:6379> lpush list two

(integer) 3

127.0.0.1:6379> lrem list 2 two

(integer) 2

127.0.0.1:6379> lrange list 0 -1

1) "one"

ltrim [key] [start] [end] #擷取指定位置至結束位置的值 start開始下標 end結束下標

127.0.0.1:6379> lrange list 0 -1

1) "hello"

2) "hell01"

3) "hell02"

4) "hell03"

127.0.0.1:6379> ltrim list 1 2

ok127.0.0.1:6379> lrange list 0 -1

1) "hell01"

2) "hell02"

rpoplpush [source] [destination] #把指定source (list)push到 目標list(destination)

127.0.0.1:6379> lrange mylist 0 -1

1) "hello"

2) "hello1"

3) "hello2"

4) "hello4"

127.0.0.1:6379> rpoplpush mylist yourlist

"hello4"

127.0.0.1:6379> lrange mylist 0 -1

1) "hello"

2) "hello1"

3) "hello2"

127.0.0.1:6379> lrange yourlist 0 -1

1) "hello4"

lset [key] [index] [value]  #類似於mysql的update index是list的索引 index必須存在才能這樣用

127.0.0.1:6379> lset list 0 item

ok127.0.0.1:6379> lindex list 0

"item"

127.0.0.1:6379> lset list 1 v1

(error) err index out of range

linsert [keyname] before|after [value1] [value] #在list指定位置前面或者後面插入值 value1list中的乙個值 value插入的值

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> lrange mylist 0 -1

1) "hello"

2) "hello1"

3) "hello2"

127.0.0.1:6379> linsert mylist before hello1 v1

(integer) 4

127.0.0.1:6379> lrange mylist 0 -1

1) "hello"

2) "v1"

3) "hello1"

4) "hello2"

lpush rpop 訊息佇列 lpush lpop棧 list運用

Redis學習筆記之List

1 list基本命令 rpush和lpush 從右 左邊插入乙個字串 lpop和rpop 從左 右邊彈出乙個字串 lindex 從給定位置區的乙個字串 lrange 從list中取得一定範圍的字串 llen 返回list的長度 2 list高階命令 ltrim 讓list只保留指定區間內的元素 li...

Redis學習筆記(二) list結構及命令詳解

書接上文redis學習筆記 一 2 list結構及命令詳解 1.lpush key value從左面往煉表中新增 rpush key value 從右面新增 2.lrange key start stop 返回鍊錶中 start,stop 中的元素 3.lpop rpop彈出元素 4.lrem ke...

redis學習筆記 二

1.redis中命令大小寫不敏感 2.redis的啟動 sudo service redis server start redis cli 啟動客戶端 exists key 判斷乙個key是否存在,存在返回1,不存在返回0 del key 刪除乙個key,或者一系列的kye del key1 key...