Redis:命令:修订间差异

来自WHY42
Riguz留言 | 贡献
Riguz留言 | 贡献
第122行: 第122行:
3) "3"
3) "3"
</syntaxhighlight>
</syntaxhighlight>
===阻塞操作===
"wait for elements in the list tasks, but return if after 5 seconds no element is available"
<syntaxhighlight lang="bash">
> brpop tasks 5
1) "tasks"
2) "do_something"
</syntaxhighlight>
Notes:
* It is possible to build safer queues or rotating queues using <span class="article-label">LMOVE</span>.
* There is also a blocking variant of the command, called <span class="article-label">BLMOVE</span>.
[[Category:Database]]
[[Category:Database]]

2021年5月6日 (四) 02:17的版本

基本操作

增删改查

设置key的value

> set mykey somevalue
OK
> get mykey
"somevalue"

设置的时候支持附加条件,仅在value不存在(或者存在)的时候才设置值

> set mykey newval nx
(nil)
> set mykey newval xx
OK

数字操作

可通过 , , and 等操作数字:

> 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

从list中取出数据

> rpush mylist a b c
(integer) 3
> rpop mylist
"c"
> rpop mylist
"b"
> rpop mylist
"a"
> rpop mylist
(nil)

裁剪list

> rpush mylist 1 2 3 4 5
(integer) 5
> ltrim mylist 0 2
OK
> lrange mylist 0 -1
1) "1"
2) "2"
3) "3"

阻塞操作

"wait for elements in the list tasks, but return if after 5 seconds no element is available"

> brpop tasks 5
1) "tasks"
2) "do_something"

Notes:

  • It is possible to build safer queues or rotating queues using .
  • There is also a blocking variant of the command, called .