Redis common commands quick check
Redis mainly has 5 data types, including String, List, Set, Zset, Hash, which can meet most of the usage requirements
type of data | Storeable Value | operating | Application scenario |
---|---|---|---|
String | String, integer, floating point | Operate the entire string or part of it; increase and decrease the logarithm |
Cache current limit counter Distributed lock Distributed Sesssion |
Hash | Unordered hash table containing key-value pairs | Add, get, and remove a single key-value pair; get all key-value pairs; check whether a key exists |
User information page visits |
List | List | Push and pop elements in the first place; operate on some elements |
queue |
Set | Unordered collection | Add, get, and remove a single element; check if the element exists; calculate intersection, union, and difference set; get elements randomly from the set |
Like, dislike, tag intersection, union, difference operation |
ZSet | Ordered set | Add, get, and remove a single element; get the element according to the score range or member to calculate the ranking of a key |
To re- ordering (ranking) |
1. String type commonly used commands
The subscripts in all commands of String are calculated from 0
set key value 给key设置值value
get key 获取key
getrange key start end 字符串分段(下标从0开始,双闭包,end为-1表示取start到所有)
getset key new_value 覆盖值,返回旧值
mset k1 v1 [k2 v2…] 批量设置key value
mget k1 [k2…] 批量获取
msetnx k1 v1 [k2 v2 …] 为多个键分别设置它们的值,仅当键不存在时
setnx key value 设置键的值,仅当键不存在时设置
setex key seconds value 设置键值并设置过期时间秒(expire)
setrange key index value 从index开始替换为value(value有几位替换几位,返回为替换后的key长度)
incr age 递增
incrby age 10 递增,指定步幅10
decr age 递减
decrby age 10 递减,指定步幅10
incrbyfloat key float 增减浮点数(返回计算后的值)
append key value2 追加
strlen key 获取长度
pesetex key milliseconds value 设置键的值和到期时间(以毫秒为单位)
getbit/setbit/bitcount/bitop 位操作
2. Hash type commonly used commands
hset key field value 设置散列字段的字符串值
hget key field 获取存储在指定键的哈希字段的值
hdel key field2 [field2] 删除一个或多个哈希字段
hexists key field 判断是否存在散列字段(1存在,0不存在)
hgetall key 获取该hash中所有的属性
hmset key f1 v1 [f2 v2…] 为多个哈希字段分别设置它们的值
hmget key f1 [f2…] 获取多个给定哈希字段的值
hsetnx key f v 仅当字段不存在时,才设置散列字段的值
hincrby key field num 按num大小递增
hdel myhash name 删除
hkeys key 获取哈希中的所有字段
hvals key 获取哈希中的所有值
hlen key 获取哈希中长度
3. List type commonly used commands
lpush mylist a b c 将一个或多个值添加到列表,左(头)插入
rpush mylist x y z 将一个或多个值添加到列表,右(尾)插入
lrange mylist s1 e1 获取数据集合(闭包[s1,e1],e1=-1表示表示取start到所有)
lpop mylist 弹出元素,左(头)开始第一个
rpop mylist 弹出元素,右(尾)开始第一个
llen mylist 获取长度
lrem mylist count value 从列表中删除元素,详见注解1
lindex mylist index 通过其索引从列表获取元素
lset mylist index valuse 通过索引设值
ltrim mylist s1 e1 列表只保留[s1,e1]之间的元素
linsert mylist before|after v1 v2 插入,注解2
rpoplpush list list2 转移列表的数据
lpushx list value 仅当列表存在时才左插入
rpushx list value 仅当列表存在时才右插入
annotation:
1.lrem mylist count value :
The parameter count
values, the parameter list to remove value
elements equal.
count
The value of can be the following:
count > 0
: Starting from the end of the header to the table search, removal andvalue
equal elements, in an amount ofcount
.count < 0
: Start the search from the head end of the table to the table, to remove withvalue
equal elements, as the numbercount
of absolute values.count = 0
: Remove all tablevalue
values equal.
2.linsert mylist before|after v1 v2
V2 inserted into the list a value key
which is located before or after the value v1. When the index is not present in the list key
, the no action.
4. Common Commands of Set Type
sadd key v1[v2…] 将一个或多个成员添加到集合
smembers key 获取数据集合全部元素
srem key v1[v2…] 从集合中删除一个或多个成员
spop 从集合中删除并返回随机一个成员
sismember key v 判断确定给定值是否是集合的成员
scard key 获取集合中的成员数
sdiff | sinter | sunion key1 [key2] 集合间运算:差集 | 交集 | 并集
sdiffstore|sinterstore|sunionstore newkey key1 [key2] 差集 | 交集 | 并集 并将结果集存储在newkey键中
srandmember key [count] 从集合中获取一个或多个随机成员
smove oldkey newkey v 将成员从一个集合移动到另一个集合
5. Common commands of ZSet type
zadd key 1 v1
zadd key 2 v2
zadd key 3 v3 为有序集合key赋值与值权重
zincrby key score v 为某值增长分数score
zscore key v 获取某值分数
zrem key v1 [v2…] 删除一个或多个元素
zcard key 元素数量
zrange key s e [withscores] 返回有序集 key 中,指定区间内的成员递增(加withscores为同时返回值的scores)
zrangebyscorekey min max [withscores] [LIMIT offset count] 返回指定范围的值,顺序小到大(注解1)
zrevrangebyscore key max min [WITHSCORES] [LIMIT offset count] 指定范围的值,逆序
zcount min max 获得指定分数范围[minx,max]内的元素个数
zremrangebyrank key start stop 按照排名范围删除元素,注解2
zremrangebyscore key min max 按照分数范围[minx,max]删除元素
zrank key v 返回有序集 key 中成员 v 的排名,其中有序集成员按 score 值递减(从小到大)排序。
zrevrank key v 返回有序集 key 中成员 member 的排名,其中有序集成员按 score 值递减(从大到小)排序。
ZUNIONSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX] 求并集,注解3
zinterstore destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX] 求交集,含义同上
annotation:
1.key min max [WITHSCORES] [LIMIT offset count] :
Returns the ordered set key
, all score
values between min
and max
(including between equal min
or max
) members. Ordered set by a member of score
increment value (small to large) arranged in the order.
The optional LIMIT
parameters used for paging.
The optional WITHSCORES
parameter determines just returned result set is a member of an ordered set, or the ordered set of members and their score
value is returned together.
2.zremrangebyrank key start stop :
Remove the ordered set key
, all members in the interval specified rank (rank).
The following sections are standard parameters start
and stop
noted that contained start
and stop
included.
Subscript parameters start
and stop
are at 0
a bottom, that is to say, to 0
an ordered set of the first member to 1
an ordered set of the second member, and so on. You can also use a negative index to -1
indicate the last member, -2
he represents the penultimate members, and so on.
3.ZUNIONSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX]
Calculation given one or more ordered sets and sets, wherein a given key
number must be numkeys
specified parameters, and the union (the result set) to the storage destination
.
By default, a member of the result set score
values are all members of a given set this score
value and .
Use WEIGHTS
option, you can each given ordered set are designated a multiplication factor (multiplication factor), all members of each given an ordered set of score
values before being passed on to an aggregate function (aggregation function) must first multiplied The factor of this ordered set.
If no WEIGHTS
option is the default setting for the multiplication factor 1
.
Use AGGREGATE
aggregation option, you can specify and set the result set.
The default parameters used SUM
, all members of the set may be a score
value , and as a result set of the member score
value; parameter MIN
may be a member of all sets the minimum score
value as the membership in the result set score
value; the parameter MAX
is It is a collection of all the members of the maximum score
value as a result of the member's focus on score
value.
Save to: the return value of destination
the base of the result set.
For specific usage details and other commonly used commands, please refer to this website: Redis Command Reference