[关闭]
@xtccc 2018-06-15T19:43:10.000000Z 字数 5385 阅读 1197

Commands

Redis


目录




keys


精确查找
[redis-server.internal.gridx.com:6379> set tao 'TAO'
OK

[redis-server.internal.gridx.com:6379> set xiao 'XIAO'
OK

[redis-server.internal.gridx.com:6379> get tao
"TAO"

模糊匹配
[redis-server.internal.gridx.com:6379> keys *
1) "tao"
2) "xiao"

[redis-server.internal.gridx.com:6379> keys xiao
1) "xiao"

[redis-server.internal.gridx.com:6379> keys x?o
(empty list or set)

[redis-server.internal.gridx.com:6379> keys x*o
1) "xiao"

[redis-server.internal.gridx.com:6379> keys x??o
1) "xiao"

[redis-server.internal.gridx.com:6379> keys xi[abc]o
1) "xiao"

[redis-server.internal.gridx.com:6379> keys xi[^a]o
(empty list or set)

[redis-server.internal.gridx.com:6379> keys xi[^b]o
1) "xiao"

删除Keys
When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash.

redis-server.internal.gridx.com:6379> DEL xl X my XL
(integer) 4
删除不存在的key并不报错
redis-server.internal.gridx.com:6379> DEL NOKEY
(integer) 0

DUMP导出并序列化指定key的value
The returned value can be synthesized back into a Redis key using the RESTORE command.

redis-server.internal.gridx.com:6379> LRANGE LS 0 -1
1) "WORLD"
2) "HELLO"

redis-server.internal.gridx.com:6379> DUMP LS
"\x0e\x01\x19\x19\x00\x00\x00\x11\x00\x00\x00\x02\x00\x00\x05WORLD\a\x05HELLO\xff\a\x00hD\xf2\xb4f\x87&,"
redis-server.internal.gridx.com:6379>

redis-server.internal.gridx.com:6379> DUMP no
(nil)

RESTORE可以实现反序列化,并创建一个list
redis-server.internal.gridx.com:6379> DEL LS
(integer) 1

redis-server.internal.gridx.com:6379> RESTORE LS 0 "\x0e\x01\x19\x19\x00\x00\x00\x11\x00\x00\x00\x02\x00\x00\x05WORLD\a\x05HELLO\xff\a\x00hD\xf2\xb4f\x87&,"
OK

redis-server.internal.gridx.com:6379> LRANGE LS 0 -1
1) "WORLD"
2) "HELLO"

redis-server.internal.gridx.com:6379> TYPE LS
list

遍历Keys

SCAN会遍历当前Redis Database中的全部keys。cursor从0开始,每次调用SCAN后,会返回新的cursor,我们下一次调用SCAN时把这个新的cursor作为参数。当返回的cursor为0时,表示已经遍历结束了。

排序
redis-server.internal.gridx.com:6379> LRANGE xs 0 -1
1) "3"
2) "-10"
3) "4"
4) "-1"
5) "2"
6) "3"

redis-server.internal.gridx.com:6379> SORT xs ASC
1) "-10"
2) "-1"
3) "2"
4) "3"
5) "3"
6) "4"

如果带有LIMIT,则首先对整个List排序,然后再返回指定范围的元素
SORT xs ASC LIMIT 2 4
1) "2"
2) "3"
3) "3"
4) "4"




List


LPUSH, RPUSH, LPOP, RPOP, LINDEX, LINSERT
LPUSHX, LSET

LPUSH将一个元素插入到列表,如果列表本来不存在,则先创建
redis-server.internal.gridx.com:6379> LPUSH xl "A"
(integer) 1

List不能用GET取出
redis-server.internal.gridx.com:6379> GET xl
(error) WRONGTYPE Operation against a key holding the wrong kind of value

LRANGE按照索引范围来取值(从头到尾的顺序)
L指List,不存在RRANGE
redis-server.internal.gridx.com:6379> LRANGE xl 0 -1
1) "A"

返回值是插入后的列表长度
redis-server.internal.gridx.com:6379> LPUSH xl "B"
(integer) 2

redis-server.internal.gridx.com:6379> LRANGE xl 0 1
1) "B"
2) "A"

redis-server.internal.gridx.com:6379> LRANGE xl 0 1
1) "B"
2) "A"

redis-server.internal.gridx.com:6379> LRANGE xl 0 2
1) "B"
2) "A"

redis-server.internal.gridx.com:6379> LRANGE xl 1 0
(empty list or set)

一次性插入多个元素
redis-server.internal.gridx.com:6379> LPUSH xl "hello" "world" "i'm fine"
(integer) 5

redis-server.internal.gridx.com:6379> LRANGE xl 0 -1
1) "i'm fine"
2) "world"
3) "hello"
4) "B"
5) "A"

RPUSH是从队列的尾部插入(apoend)
redis-server.internal.gridx.com:6379> RPUSH xl 10
(integer) 6

redis-server.internal.gridx.com:6379> RPUSH xl 20
(integer) 7

redis-server.internal.gridx.com:6379> LRANGE xl 0 -1
1) "i'm fine"
2) "world"
3) "hello"
4) "B"
5) "A"
6) "10"
7) "20"

LPOP是将List的头部元素删除,并返回该元素
redis-server.internal.gridx.com:6379> LPOP xl
"i'm fine"
redis-server.internal.gridx.com:6379> LRANGE xl 0 -1
1) "world"
2) "hello"
3) "B"
4) "A"
5) "10"
6) "20"

LLEN返回列表长度
redis-server.internal.gridx.com:6379> LLEN xl
(integer) 6

RPOP删除并返回尾部的元素
redis-server.internal.gridx.com:6379> RPOP XL
"hello "
redis-server.internal.gridx.com:6379> RPOP xl
"20"

可以向LIST插入不同类型的元素
redis-server.internal.gridx.com:6379> LRANGE my 0 -1
1) "true"
2) "2.2"
3) "b"
4) "1"
5) "a"

INDEX返回指定位置的元素,从0开始计算
redis-server.internal.gridx.com:6379> LINDEX my 0
"true"
redis-server.internal.gridx.com:6379> LINDEX my 2
"b"

redis-server.internal.gridx.com:6379> LRANGE my 0 -1
1) "true"
2) "2.2"
3) "b"
4) "1"
5) "a"

将"X"插入到"b"之前,返回插入后的列表的长度
redis-server.internal.gridx.com:6379> LINSERT my BEFORE "b" "X"
(integer) 6
redis-server.internal.gridx.com:6379> LRANGE my 0 -1
1) "true"
2) "2.2"
3) "X"
4) "b"
5) "1"
6) "a"

如果找不到pivot插入失败,返回-1
redis-server.internal.gridx.com:6379> LINSERT my BEFORE "NO" "HI"
(integer) -1

从pivot后面插入
redis-server.internal.gridx.com:6379> LINSERT my AFTER "b" "HI"
(integer) 7

redis-server.internal.gridx.com:6379> LRANGE my 0 -1
1) "true"
2) "2.2"
3) "X"
4) "b"
5) "HI"
6) "1"
7) "a"

只有当L1是一个已存在的非空列表时,才能将"hello"插在L1的开头处
插入成功,返回插入后的列表长度,否则返回0
RPUSH同理,只不过试图插入到列表的尾部
redis-server.internal.gridx.com:6379> LPUSHX L1 "hello"
(integer) 0
redis-server.internal.gridx.com:6379> LRANGE L1 0 -1
(empty list or set)

用LREN删除元素
redis-server.internal.gridx.com:6379> LRANGE L 0 -1
1) "A"
2) "A"
3) "B"
4) "Y"
5) "X"
6) "B"
7) "B"
8) "C"
9) "C"
10) "C"
11) "D"
12) "D"

按照队列中从尾到头的顺序,删除2个“B”(这两个"B"可以不连续)
redis-server.internal.gridx.com:6379> LRANGE L 0 -1
1) "A"
2) "A"
3) "X"
4) "A"
5) "Y"
6) "A"
7) "B"
从尾向头,删除2个“A”
LREM L 2 "A" 则表示从头向尾删除
redis-server.internal.gridx.com:6379> LREM L -2 "A"
(integer) 2 返回实际删除元素的个数
redis-server.internal.gridx.com:6379> LRANGE L 0 -1
1) "A"
2) "A"
3) "X"
4) "Y"
5) "B"

LSET用于重置列表的某个位置的值
redis-server.internal.gridx.com:6379> RPUSH xs "One" "Two" "Three"
(integer) 3
redis-server.internal.gridx.com:6379> LRANGE xs 0 -1
1) "One"
2) "Two"
3) "Three"

redis-server.internal.gridx.com:6379> LSET xs 0 "Hello"
OK

redis-server.internal.gridx.com:6379> LRANGE xs 0 -1
1) "Hello"
2) "Two"
3) "Three"

redis-server.internal.gridx.com:6379> LSET xs -1 "World"
OK

redis-server.internal.gridx.com:6379> LRANGE xs 0 -1
1) "Hello"
2) "Two"
3) "World"

LTRIM 是保留指定位置的若干元素
redis-server.internal.gridx.com:6379> LRANGE xs 0 -1
1) "B"
2) "A"
3) "Hello"
4) "Two"
5) "World"
redis-server.internal.gridx.com:6379> LTRIM xs 1 2
OK
只保留原来位置0和位置1的两个元素
redis-server.internal.gridx.com:6379> LRANGE xs 0 -1
1) "A"
2) "Hello"

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注