Redis:命令:修订间差异
第134行: | 第134行: | ||
* It is possible to build safer queues or rotating queues using <span class="article-label">LMOVE</span>. | * 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>. | * There is also a blocking variant of the command, called <span class="article-label">BLMOVE</span>. | ||
==Hash操作== | |||
===设置及查询=== | |||
<syntaxhighlight lang="bash"> | |||
> hmset user:1000 username antirez birthyear 1977 verified 1 | |||
OK | |||
> hget user:1000 username | |||
"antirez" | |||
> hget user:1000 birthyear | |||
"1977" | |||
> hgetall user:1000 | |||
1) "username" | |||
2) "antirez" | |||
3) "birthyear" | |||
4) "1977" | |||
5) "verified" | |||
6) "1" | |||
</syntaxhighlight> | |||
[[Category:Database]] | [[Category:Database]] |
2021年5月6日 (四) 02:19的版本
基本操作
增删改查
设置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
从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 LMOVE.
- There is also a blocking variant of the command, called BLMOVE.
Hash操作
设置及查询
> hmset user:1000 username antirez birthyear 1977 verified 1
OK
> hget user:1000 username
"antirez"
> hget user:1000 birthyear
"1977"
> hgetall user:1000
1) "username"
2) "antirez"
3) "birthyear"
4) "1977"
5) "verified"
6) "1"