Redis操作命令

Posted by Sunfy on 2019-10-19
Words 3.6k and Reading Time 17 Minutes
Viewed Times
Viewed Times
Visitors In Total

Redis基本数据结构及常用操作命令以及对应的使用场景

Redis 最重要的一部分主题(topic)文档, 比如事务、持久化、复制、Sentinel、集群等

你可以通过网址 doc.redisfans.com 在线阅览本文档, 也可以下载 PDF 格式 或者 HTML 格式 的离线版本。

字符串(String)

常用操作命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
help @String
APPEND key value
summary: Append a value to a key(向键追加一个值)
since: 2.0.0
BITCOUNT key [start end]
summary: Count set bits in a string(对字符串中的位进行计数)
since: 2.6.0
BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
summary: Perform arbitrary bitfield integer operations on strings(对字符串执行任意位域整数操作)
since: 3.2.0
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings(在字符串之间执行按位操作)
since: 2.6.0
BITPOS key bit [start] [end]
summary: Find first bit set or clear in a string(查找字符串中设置或清除的第一个位)
since: 2.8.7
DECR key
summary: Decrement the integer value of a key by one(键的整数值递减1)
since: 1.0.0
DECRBY key decrement
summary: Decrement the integer value of a key by the given number(按给定的数字递减键值的整数值)
since: 1.0.0
GET key
summary: Get the value of a key(获取键的值)
since: 1.0.0
GETBIT key offset
summary: Returns the bit value at offset in the string value stored at key(返回存储在键中的字符串值的偏移位值)
since: 2.2.0
GETRANGE key start end
summary: Get a substring of the string stored at a key(获取存储在键上的字符串的子字符串)
since: 2.4.0
GETSET key value
summary: Set the string value of a key and return its old value(设置键的字符串值并返回其旧值)
since: 1.0.0
INCR key
summary: Increment the integer value of a key by one(将键的整数值增加1)
since: 1.0.0
INCRBY key increment
summary: Increment the integer value of a key by the given amount(将键的整数值按给定的量递增)
since: 1.0.0
INCRBYFLOAT key increment
summary: Increment the float value of a key by the given amount(将键的浮点值按给定的数量递增)
since: 2.6.0
MGET key [key ...]
summary: Get the values of all the given keys(获取所有给定键的值)
since: 1.0.0
MSET key value [key value ...]
summary: Set multiple keys to multiple values(将多个键设置为多个值)
since: 1.0.1
MSETNX key value [key value ...]
summary: Set multiple keys to multiple values, only if none of the keys exist(将多个键设置为多个值,仅当这些键不存在时才设置)
since: 1.0.1
PSETEX key milliseconds value
summary: Set the value and expiration in milliseconds of a key(置键的值和到期时间(以毫秒为单位))
since: 2.6.0
SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
summary: Set the string value of a key(设置键的字符串值)
since: 1.0.0
SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key(设置或清除存储在键上的字符串值中的偏移位)
since: 2.2.0
SETEX key seconds value
summary: Set the value and expiration of a key(设置键的值和过期时间)
since: 2.0.0
SETNX key value
summary: Set the value of a key, only if the key does not exist(设置键值,仅当键不存在时才设置)
since: 1.0.0
SETRANGE key offset value
summary: Overwrite part of a string at key starting at the specified offset(覆盖从指定偏移量开始的键处的字符串部分)
since: 2.2.0
STRLEN key
summary: Get the length of the value stored in a key(获取存储在键中的值的长度)
since: 2.2.0

使用场景

  • 单值缓存
  • 对象缓存
  • 分布式锁
  • 计数器
  • Web集群session共享
  • 分布式系统全局序列号

Hash结构

常用操作命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
help @Hash
HDEL key field [field ...]
summary: Delete one or more hash fields(删除一个或多个散列字段)
since: 2.0.0
HEXISTS key field
summary: Determine if a hash field exists(确定散列字段是否存在)
since: 2.0.0
HGET key field
summary: Get the value of a hash field(获取散列字段的值)
since: 2.0.0
HGETALL key
summary: Get all the fields and values in a hash(获取散列中的所有字段和值)
since: 2.0.0
HINCRBY key field increment
summary: Increment the integer value of a hash field by the given number(将哈希字段的整数值按给定的数字递增)
since: 2.0.0
HINCRBYFLOAT key field increment
summary: Increment the float value of a hash field by the given amount(将哈希字段的浮点值按给定的数量递增)
since: 2.6.0
HKEYS key
summary: Get all the fields in a hash(获取散列中的所有字段)
since: 2.0.0
HLEN key
summary: Get the number of fields in a hash(获取散列中字段的数量)
since: 2.0.0
HMGET key field [field ...]
summary: Get the values of all the given hash fields(获取所有给定哈希字段的值)
since: 2.0.0
HMSET key field value [field value ...]
summary: Set multiple hash fields to multiple values(将多个哈希字段设置为多个值)
since: 2.0.0
HSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate hash fields and associated values(增量迭代哈希字段和关联值)
since: 2.8.0
HSET key field value
summary: Set the string value of a hash field(设置散列字段的字符串值)
since: 2.0.0
HSETNX key field value
summary: Set the value of a hash field, only if the field does not exist(设置散列字段的值,仅当该字段不存在时才设置该值)
since: 2.0.0
HSTRLEN key field
summary: Get the length of the value of a hash field(获取散列字段值的长度)
since: 3.2.0
HVALS key
summary: Get all the values in a hash(获取散列中的所有值)
since: 2.0.0

使用场景

  • 对象缓存
  • 电商购物车

优缺点

优点
1)同类数据归类整合储存,方便数据管理
2)相比string操作消耗内存与CPU更小
3)相比string储存更节省空间

缺点
1)过期功能不能使用在field上,只能用在key上
2)Redis集群架构下不适合大规模使用

List结构

常用操作命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
help @List
BLPOP key [key ...] timeout
summary: Remove and get the first element in a list, or block until one is available(删除并获取列表或块中的第一个元素,直到有一个可用为止)
since: 2.0.0
BRPOP key [key ...] timeout
summary: Remove and get the last element in a list, or block until one is available(删除并获取列表或块中的最后一个元素,直到有一个可用为止)
since: 2.0.0
BRPOPLPUSH source destination timeout
summary: Pop a value from a list, push it to another list and return it; or block until one is available(从列表中弹出一个值,将其推到另一个列表并返回;或阻塞,直到可用为止)
since: 2.2.0
LINDEX key index
summary: Get an element from a list by its index(通过索引从列表中获取元素)
since: 1.0.0
LINSERT key BEFORE|AFTER pivot value
summary: Insert an element before or after another element in a list(在列表中,在另一个元素之前或之后插入一个元素)
since: 2.2.0
LLEN key
summary: Get the length of a list(获取列表的长度)
since: 1.0.0
LPOP key
summary: Remove and get the first element in a list(删除并获取列表中的第一个元素)
since: 1.0.0
LPUSH key value [value ...]
summary: Prepend one or multiple values to a list(在列表前添加一个或多个值)
since: 1.0.0
LPUSHX key value
summary: Prepend a value to a list, only if the list exists(仅当列表存在时,在列表前加上一个值)
since: 2.2.0
LRANGE key start stop
summary: Get a range of elements from a list(从列表中获取元素的范围)
since: 1.0.0
LREM key count value
summary: Remove elements from a list(从列表中删除元素)
since: 1.0.0
LSET key index value
summary: Set the value of an element in a list by its index(通过索引设置列表中元素的值)
since: 1.0.0
LTRIM key start stop
summary: Trim a list to the specified range(将列表修剪到指定的范围)
since: 1.0.0
RPOP key
summary: Remove and get the last element in a list(删除并获取列表中的最后一个元素)
since: 1.0.0
RPOPLPUSH source destination
summary: Remove the last element in a list, prepend it to another list and return it(删除列表中的最后一个元素,将其添加到另一个列表并返回)
since: 1.2.0
RPUSH key value [value ...]
summary: Append one or multiple values to a list(向列表追加一个或多个值)
since: 1.0.0
RPUSHX key value
summary: Append a value to a list, only if the list exists(仅当列表存在时,向列表追加一个值)
since: 2.2.0

使用场景

  • 常用数据结构
1
2
3
Stack(栈) = LPUSH + LPOP
Queue(队列)= LPUSH + RPOP
Blocking MQ(阻塞队列)= LPUSH + BRPOP
  • 微博和微信公号消息流
  • 微博消息和微信公号消息

Set结构

常用操作命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
help @Set
SADD key member [member ...]
summary: Add one or more members to a set(向集合中添加一个或多个成员)
since: 1.0.0
SCARD key
summary: Get the number of members in a set(获取一个集合中的成员数)
since: 1.0.0
SDIFF key [key ...]
summary: Subtract multiple sets(减去多个集合)
since: 1.0.0
SDIFFSTORE destination key [key ...]
summary: Subtract multiple sets and store the resulting set in a key(减去多个集合并将结果集存储在一个键中)
since: 1.0.0
SINTER key [key ...]
summary: Intersect multiple sets(交叉多个集合)
since: 1.0.0
SINTERSTORE destination key [key ...]
summary: Intersect multiple sets and store the resulting set in a key(交叉多个集合并将结果集存储在一个键中)
since: 1.0.0
SISMEMBER key member
summary: Determine if a given value is a member of a set(确定给定值是否是集合的成员)
since: 1.0.0
SMEMBERS key
summary: Get all the members in a set(获取集合中的所有成员)
since: 1.0.0
SMOVE source destination member
summary: Move a member from one set to another(将成员从一个集合移动到另一个集合)
since: 1.0.0
SPOP key [count]
summary: Remove and return one or multiple random members from a set(从一个集合中移除并返回一个或多个随机成员)
since: 1.0.0
SRANDMEMBER key [count]
summary: Get one or multiple random members from a set(从一个集合中随机获取一个或多个成员)
since: 1.0.0
SREM key member [member ...]
summary: Remove one or more members from a set(从集合中删除一个或多个成员)
since: 1.0.0
SSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate Set elements(增量迭代集元素)
since: 2.8.0
SUNION key [key ...]
summary: Add multiple sets(添加多个集合)
since: 1.0.0
SUNIONSTORE destination key [key ...]
summary: Add multiple sets and store the resulting set in a key(添加多个集合并将结果集存储在一个键中)
since: 1.0.0

使用场景

  • 微信微博点赞,收藏,标签
  • 集合操作
  • 集合操作实现微博微信关注模型
  • 集合操作实现电商商品筛选
  • 微信抽奖小程序

ZSet有序集合结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
help @sorted_set
BZPOPMAX key [key ...] timeout
summary: Remove and return the member with the highest score from one or more sorted sets, or block until one is available(从一个或多个已排序集合或块中移除并返回得分最高的成员,直到有一个可用为止)
since: 5.0.0
BZPOPMIN key [key ...] timeout
summary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is available(删除并返回一个或多个已排序集合或块中得分最低的成员,直到有一个可用为止)
since: 5.0.0
ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
summary: Add one or more members to a sorted set, or update its score if it already exists(向已排序的集合中添加一个或多个成员,如果已经存在,则更新其分数)
since: 1.2.0
ZCARD key
summary: Get the number of members in a sorted set(获取已排序集合中的成员数)
since: 1.2.0
ZCOUNT key min max
summary: Count the members in a sorted set with scores within the given values(在给定值内对排序集合中的成员进行计数)
since: 2.0.0
ZINCRBY key increment member
summary: Increment the score of a member in a sorted set(递增排序集合中成员的分数)
since: 1.2.0
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
summary: Intersect multiple sorted sets and store the resulting sorted set in a new key(交叉多个排序集,并将结果排序集存储到一个新键中)
since: 2.0.0
ZLEXCOUNT key min max
summary: Count the number of members in a sorted set between a given lexicographical range(计算给定字典法范围之间已排序集合中的成员数量)
since: 2.8.9
ZPOPMAX key [count]
summary: Remove and return members with the highest scores in a sorted set(删除并返回排序集中得分最高的成员)
since: 5.0.0
ZPOPMIN key [count]
summary: Remove and return members with the lowest scores in a sorted set(删除并返回排序集中得分最低的成员)
since: 5.0.0
ZRANGE key start stop [WITHSCORES]
summary: Return a range of members in a sorted set, by index(按索引返回排序集中的成员范围)
since: 1.2.0
ZRANGEBYLEX key min max [LIMIT offset count]
summary: Return a range of members in a sorted set, by lexicographical range(按字典顺序范围返回已排序集合中成员的范围)
since: 2.8.9
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
summary: Return a range of members in a sorted set, by score(按分数返回排序集中的成员范围)
since: 1.0.5
ZRANK key member
summary: Determine the index of a member in a sorted set(确定排序集合中成员的索引)
since: 2.0.0
ZREM key member [member ...]
summary: Remove one or more members from a sorted set(从已排序的集合中删除一个或多个成员)
since: 1.2.0
ZREMRANGEBYLEX key min max
summary: Remove all members in a sorted set between the given lexicographical range(删除给定字典排序范围之间已排序集合中的所有成员)
since: 2.8.9
ZREMRANGEBYRANK key start stop
summary: Remove all members in a sorted set within the given indexes(删除给定索引中排序集合中的所有成员)
since: 2.0.0
ZREMRANGEBYSCORE key min max
summary: Remove all members in a sorted set within the given scores(删除给定分数内排序集合中的所有成员)
since: 1.2.0
ZREVRANGE key start stop [WITHSCORES]
summary: Return a range of members in a sorted set, by index, with scores ordered from high to low(按索引返回排序集合中成员的范围,分数从高到低排序)
since: 1.2.0
ZREVRANGEBYLEX key max min [LIMIT offset count]
summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.(按字典顺序从较高的字符串到较低的字符串,返回已排序集合中成员的范围。)
since: 2.8.9
ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
summary: Return a range of members in a sorted set, by score, with scores ordered from high to low(按分数返回排序集中的成员范围,分数从高到低排序)
since: 2.2.0
ZREVRANK key member
summary: Determine the index of a member in a sorted set, with scores ordered from high to low(根据分数从高到低的顺序,确定排序集合中成员的索引)
since: 2.0.0
ZSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate sorted sets elements and associated scores(增量迭代已排序的集合元素和相关分数)
since: 2.8.0
ZSCORE key member
summary: Get the score associated with the given member in a sorted set(获取排序集中与给定成员关联的分数)
since: 1.2.0
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
summary: Add multiple sorted sets and store the resulting sorted set in a new key(添加多个排序集,并将结果排序集存储到一个新键中)
since: 2.0.0

使用场景

  • ZSet集合操作实现排行榜

Copyright 2021 sunfy.top ALL Rights Reserved

...

...

00:00
00:00