Redis List
- Redis lists are implemented via Linked Lists.
- Accessing an element by index is very fast in lists implemented with an Array and not so fast in lists implemented by linked lists.
LPUSH RPUSH
http://redisdoc.com/list/lpush.html http://redisdoc.com/list/rpush.html
LPUSH key value [value …]
127.0.0.1:6379> lpush listA 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lpush listA 5 4 3 2 1
(integer) 10
127.0.0.1:6379> lrange listA 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "5"
7) "4"
8) "3"
9) "2"
10) "1"
127.0.0.1:6379> rpush listA a b c
(integer) 13
127.0.0.1:6379> lrange listA 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "5"
7) "4"
8) "3"
9) "2"
10) "1"
11) "a"
12) "b"
13) "c"
LPUSHX key value
http://redisdoc.com/list/lpushx.html
和 LPUSH key value [value …] 命令相反, 當 key 不存在時, LPUSHX 命令什麼都不做
RPOP
http://redisdoc.com/list/rpop.html
RPOP key
127.0.0.1:6379> rpop listA
"c"
lrange listA 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "5"
7) "4"
8) "3"
9) "2"
10) "1"
11) "a"
12) "b"
LTRIM
http://redisdoc.com/list/ltrim.html
LTRIM key start stop
127.0.0.1:6379> lpush alpha a b c d e f g
(integer) 7
127.0.0.1:6379> lrange alpha 0 -1
1) "g"
2) "f"
3) "e"
4) "d"
5) "c"
6) "b"
7) "a"
127.0.0.1:6379> ltrim alpha 0 5
OK
127.0.0.1:6379> lrange alpha 0 -1
1) "g"
2) "f"
3) "e"
4) "d"
5) "c"
6) "b"
LSET
http://redisdoc.com/list/lset.html
LSET key index value
127.0.0.1:6379> lrange alpha 0 -1
1) "g"
2) "f"
3) "e"
4) "d"
5) "c"
6) "b"
127.0.0.1:6379> lset alpha 0 kk
OK
127.0.0.1:6379> lrange alpha 0 -1
1) "kk"
2) "f"
3) "e"
4) "d"
5) "c"
6) "b"
LINDEX
http://redisdoc.com/list/lindex.html
LINDEX key index
127.0.0.1:6379> lrange alpha 0 -1
1) "kk"
2) "f"
3) "e"
4) "d"
5) "c"
6) "b"
127.0.0.1:6379> lindex alpha 3
"d"
LINSERT
http://redisdoc.com/list/linsert.html
LINSERT key BEFORE|AFTER pivot value
127.0.0.1:6379> lrange alpha 0 -1
1) "kk"
2) "f"
3) "e"
4) "d"
5) "c"
6) "b"
127.0.0.1:6379> linsert alpha before d yy
(integer) 7
127.0.0.1:6379> lrange alpha 0 -1
1) "kk"
2) "f"
3) "e"
4) "yy"
5) "d"
6) "c"
7) "b"
LREM
http://redisdoc.com/list/lrem.html
LREM key count value
count > 0:從表頭開始向表尾搜索,value取出的元素,數量為count。 count < 0: 從表尾開始向表頭搜索,value取出的元素,數量為count絕對值。 count = 0: 移除表中所有與value發現的值。
127.0.0.1:6379> lrange alpha 0 -1
1) "kk"
2) "f"
3) "e"
4) "yy"
5) "d"
6) "c"
7) "b"
127.0.0.1:6379> lrem alpha 1 yy
(integer) 1
127.0.0.1:6379> lrange alpha 0 -1
1) "kk"
2) "f"
3) "e"
4) "d"
5) "c"
6) "b"
127.0.0.1:6379> lpush alpha yy yy yy yy
(integer) 10
127.0.0.1:6379> lrange alpha 0 -1
1) "yy"
2) "yy"
3) "yy"
4) "yy"
5) "kk"
6) "f"
7) "e"
8) "d"
9) "c"
10) "b"
127.0.0.1:6379> lrem alpha 2 yy
(integer) 2
127.0.0.1:6379> lrange alpha 0 -1
1) "yy"
2) "yy"
3) "kk"
4) "f"
5) "e"
6) "d"
7) "c"
8) "b"
Cheat Sheet
RPUSH mylist "hello" (Insert all the specified values at the tail of the list stored at key.)
LRANGE mylist 0 -1 (Returns the specified elements of the list stored at key.)
LPUSH mylist "world" (Insert all the specified values at the head of the list stored at key.)
RPUSHX mylist "World" (Inserts value at the tail of the list stored at key, only if key already exists and holds a list.)
LPUSHX mylist "World" (Inserts value at the head of the list stored at key, only if key already exists and holds a list.)
RPOP mylist (Removes and returns the last element of the list stored at key.)
LPOP mylist (Removes and returns the first element of the list stored at key.)
LTRIM mylist 1 -1 (Trim an existing list so that it will contain only the specified range of elements specified)
LSET mylist 0 "four" (Sets the list element at index to value.)
LINDEX mylist 0 (Returns the element at index in the list stored at key.)
LINSERT mylist BEFORE "World" "There" (Inserts value in the list stored at key either before or after the reference value pivot.)
LLEN mylist (Returns the length of the list stored at key. )
LREM mylist 2 "hello" (Removes the first count occurrences of elements equal to value from the list stored at key.)