Redis:命令
基本操作
增删改查
设置key的value
> set mykey somevalue
OK
> get mykey
"somevalue"
设置的时候支持附加条件,仅在value不存在(或者存在)的时候才设置值
> set mykey newval nx
(nil)
> set mykey newval xx
OK
数字操作
可通过 INCR, INCRBY, DECR and DECRBY等操作数字:
> set counter 100
OK
> incr counter
(integer) 101
> incr counter
(integer) 102
> incrby counter 50
(integer) 152
批量设置
> mset a 10 b 20 c 30
OK
> mget a b c
1) "10"
2) "20"
3) "30"
查询key的类型及存在与否
> set mykey hello
OK
> exists mykey
(integer) 1
> del mykey
(integer) 1
> exists mykey
(integer) 0
> set mykey x
OK
> type mykey
string
> del mykey
(integer) 1
> type mykey
none
TTL操作
设置key的ttl
> set key some-value
OK
> expire key 5
(integer) 1
> get key (immediately)
"some-value"
> get key (after some time)
(nil)
可以一次性设置key和ttl:
> set key 100 ex 10
OK
> ttl key
(integer) 9
List操作
list插入及查询
> rpush mylist A
(integer) 1
> rpush mylist B
(integer) 2
> lpush mylist first
(integer) 3
> lrange mylist 0 -1
1) "first"
2) "A"
3) "B"
> rpush mylist 1 2 3 4 5 "foo bar"
(integer) 9