Redis 小得
Install
apk add redis
nohup redis-server &
redis-cli
这里使用Linux发行版alpine 3.12进行测试,即可进入redis client终端
Key
Redis 键(key) | 菜鸟教程 (runoob.com)
查
KEYS pattern
匹配所有符合pattern的key,用的规则貌似不是正则
Supported glob-style patterns:
h?llo
matcheshello
,hallo
andhxllo
h*llo
matcheshllo
andheeeello
h[ae]llo
matcheshello
andhallo,
but nothillo
h[^e]llo
matcheshallo
,hbllo
, … but nothello
h[a-b]llo
matcheshallo
andhbllo
Use
\
to escape special characters if you want to match them verbatim.Warning : consider
KEYS
as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don’t useKEYS
in your regular application code. If you’re looking for a way to find keys in a subset of your keyspace, consider usingSCAN
or sets.
127.0.0.1:6379> KEYS *
- “chairperson”
- “president”
SCAN cursor [MATCH pattern] [COUNT count]
Redis Scan 命令用于迭代数据库中的数据库键。
SCAN 命令是一个基于游标的迭代器,每次被调用之后, 都会向用户返回一个新的游标, 用户在下次迭代时需要使用这个新游标作为 SCAN 命令的游标参数, 以此来延续之前的迭代过程。
SCAN 返回一个包含两个元素的数组, 第一个元素是用于进行下一次迭代的新游标, 而第二个元素则是一个数组, 这个数组中包含了所有被迭代的元素。如果新游标返回 0 表示迭代已结束。
redis 127.0.0.1:6379> scan 0 # 使用 0 作为游标,开始新的迭代
1) "17" # 第一次迭代时返回的游标
2) 1) "key:12"
2) "key:8"
3) "key:4"
4) "key:14"
5) "key:16"
6) "key:17"
7) "key:15"
8) "key:10"
9) "key:3"
10) "key:7"
11) "key:1"
redis 127.0.0.1:6379> scan 17 # 使用的是第一次迭代时返回的游标 17 开始新的迭代
1) "0"
2) 1) "key:5"
2) "key:18"
3) "key:0"
4) "key:2"
5) "key:19"
6) "key:13"
7) "key:6"
8) "key:9"
9) "key:11"
cmd related to expiration
TTL key
:以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
PTTL key
:以毫秒为单位返回 key 的剩余的过期时间。
数据类型
这里涉及的cmd只是少数,全面的还得看Commands | Redis
String
cmd | meaning |
---|---|
SET key value [expiration EX seconds\|PX milliseconds] [NX\|XX] |
设置指定 key 的值。 |
MGET key value [key value...] |
Multi set |
GET key |
获取指定 key 的值。 |
GETSET key value |
将给定 key 的值设为 value ,并返回 key 的旧值(old value)。 |
SET 实例
NX
– Only set the key if it does not already exist.XX
– Only set the key if it already exists.SET anotherkey "will expire in a minute" EX 60 127.0.0.1:6379> SET age 10 OK 127.0.0.1:6379> GET age "10" 127.0.0.1:6379> SET age 20 NX (nil) 127.0.0.1:6379> SET youage 20 XX (nil) 127.0.0.1:6379> SET age 20 XX OK
INCR和功能相近的函数
127.0.0.1:6379> get age "20" 127.0.0.1:6379> incr age (integer) 21 127.0.0.1:6379> incrby age 2 (integer) 23 127.0.0.1:6379> INCRBYFLOAT age 0.5 "23.5" 127.0.0.1:6379> INCRBY age 5 (error) ERR value is not an integer or out of range 127.0.0.1:6379> INCR age (error) ERR value is not an integer or out of range
观察现象,浮点数无法用
INCR
和INCRBY
而只能用INCRBYFLOAT
127.0.0.1:6379> INCRBYFLOAT age 0.5 "24" 127.0.0.1:6379> INCR age (integer) 25
重新加成整数后可以用了…
127.0.0.1:6379> INCRBY age -1 (integer) 24 127.0.0.1:6379> INCRBYFLOAT age -1.5 "22.5"
负数也是可以的
Hash
cmd | meaning |
---|---|
HSET key field value |
hash set |
HMSET key field value [field value...] |
hash multiple set |
HGET key field |
hash get |
HGETALL key |
获得某一key下的所有field value对,形式是 > 1)“field1” > 2)“value1” > 3)“field2” > 4)“value2” |
HKEYS key |
返回key下field组 |
HVALS key |
返回key下value组 |
HSCAN key cursor [MATCH pattern] [COUNT count] |
hash iter, like SCAN |
这里某一key只能存储一种数据类型的数据,
127.0.0.1:6379> hmset chairperson china xjp korea jze
127.0.0.1:6379> set chairperson md
> (error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> hset chairperson india md
127.0.0.1:6379> hget chairperson india
> "md"
List
Redis 列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。
cmd | meaning |
---|---|
lpush key value [value...] |
insert in the left edge of the list |
rpush key value [value...] |
insert in the right edge of the list |
lrange key start stop |
127.0.0.1:6379> lpush chairperson mao hua hu zhao jiang hu xi
(integer) 7
# list中value的数量
127.0.0.1:6379> lrange chairperson 0 100
1) "xi"
2) "hu"
3) "jiang"
4) "zhao"
5) "hu"
6) "hua"
7) "mao"
# lpush时,每个元素都会重新插入到list的left处
127.0.0.1:6379> lrange chairperson 1 100
1) "hu"
2) "jiang"
3) "zhao"
4) "hu"
5) "hua"
6) "mao"
# lrange确实是从0作为第一个元素
127.0.0.1:6379> lrange chairperson 8 0
(empty list or set)
# 不支持倒序
Set
支持各种集合操作
cmd | meaning |
---|---|
sadd key member [member...] |
添加一个 string 元素到 key 对应的 set 集合中,成功返回 1,如果元素已经在集合中返回 0。 |
SREM key member [member ...] |
remove |
SCARD key |
获取集合的成员数 |
SMEMBERS key |
获取集合的成员 |
SISMEMBER key member |
某值是否是key的成员 |
SINTER key [key...] |
返回一个表形式的 |
SINTERSTORE destination key [key...] |
返回给定所有集合的交集并存储在 指定key destination 中 |
SUNION key [key...] |
返回一个表形式的 |
SUNIONSTORE destination key [key...] |
返回给定所有集合的并集并存储在 指定key destination 中 |
SDIFF key [key ...] |
返回第一个集合与其他集合之间的差异,见下实例 |
SSCAN key cursor [MATCH pattern] [COUNT count] |
iterate |
Sdiff 命令返回第一个集合与其他集合之间的差异,也可以认为说第一个集合中独有的元素。不存在的集合 key 将视为空集。
差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1,也不是整个 FIRST_KEY OTHER_KEY1…OTHER_KEYN 的差集。
redis> SADD key1 "a"
(integer)1
redis> SADD key1 "b"
(integer)1
redis> SADD key1 "c"
(integer)1
redis> SADD key2 "c"
(integer)1
redis> SADD key2 "d"
(integer)1
redis> SADD key2 "e"
(integer)1
redis> SDIFF key1 key2
1)"a"
2)"b"
ZSET
和SET一样也有add,rem,card,members这种统计的cmd,也有union,unionstore,diff这种集合操作
cmd | meaning |
---|---|
ZADD key [NX\|XX] [CH] [INCR] score member [score member ...] |
向有序集合添加一个或多个成员,或者更新已存在成员的分数 |
ZREM key member [member ...] |
Remove |
ZREMRANGEBYSCORE key min max |
|
ZREMRANGEBYLEX key min max |
|
ZREVRANGE key start stop [WITHSCORES] |
|
ZCARD key |
获取有序集合的成员数 |
ZCOUNT key min max |
计算在有序集合中指定区间分数的成员数 |
排序
cmd | meaning |
---|---|
ZRANGE key start stop [WITHSCORES] |
索引 |
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] |
值,LIMIT offset count,从第off条开始计数,其中0是第一个序号 |
ZRANGEBYLEX key min max [LIMIT offset count] |
返回指定字典序范围 |
ZRANK key member |
每种排序都有REV的变种,比如 ZREVRANGE
2 是返回分数从高到低的start到stop个下标。ZREVRANK
是按分数从高到低的次序
数值
cmd | meaning |
---|---|
ZINCRBY key increment member |
|
ZINTERSTORE destination numkeys key [key ...] |
ZADD参数介绍,
- NX XX就不说了,前者是Only new 后者是Only existed。
- CH是changed,即返回值的逻辑是被changed elements,Changed elements are new elements added and elements already existing for which the score was updated .而默认的逻辑是新添加的元素个数。
- INCR:提供该参数后的命令类似
ZINCRBY
,将score加上一个值,只允许提供一个member和score
高版本redis有LT GT两个,也就是less than和greater than
这里的min,max是可以有格式的,官网介绍
min and max can be -inf and +inf, so that you are not required to know the highest or lowest score in the sorted set to get all elements from or up to a certain score.
By default, the interval specified by min and max is closed (inclusive). It is possible to specify an open interval (exclusive) by prefixing the score with the character (. For example:
ZRANGEBYSCORE zset (1 5
Will return all elements with 1 < score <= 5 while:
ZRANGEBYSCORE zset (5 (10
Will return all the elements with 5 < score < 10 (5 and 10 excluded).
lex
比如说 ZRANGEBYLEX
The elements are considered to be ordered from lower to higher strings as compared byte-by-byte using the memcmp()
C function. Longer strings are considered greater than shorter strings if the common part is identical.
Valid start and stop must start with (
or [
, in order to specify if the range item is respectively exclusive or inclusive.
The special values of +
or -
for start and stop have the special meaning or positively infinite and negatively infinite strings, so for instance the command ZRANGEBYLEX myzset - + is guaranteed to return all the elements in the sorted set, if all the elements have the same score.
redis 127.0.0.1:6379> ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g
(integer) 7
redis 127.0.0.1:6379> ZRANGEBYLEX myzset - [c
1) "a"
2) "b"
3) "c"
redis 127.0.0.1:6379> ZRANGEBYLEX myzset - (c
1) "a"
2) "b"
redis 127.0.0.1:6379> ZRANGEBYLEX myzset [aaa (g
1) "b"
2) "c"
3) "d"
4) "e"
5) "f"
0 评论:
发表评论