Redis常用命令

2022/8/28 2:22:52

本文主要是介绍Redis常用命令,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

字符串string

能够存储字符串整数浮点数3种类型的值。

字符串

基本命令

+ get key //读取
+ set key value // 写入
+ del key // 删除

自增/自减

+ incr key // 键值加1
+ decr key // 键值减1
+ incrby key amount // 键值加amount
+ decrby key amount // 键值减amount
+ incrbyfloat key amount // 键值加上浮点数amount

子串

+ append key value //追加到末尾
+ getrange key start end //获取范围内的子串,包括start和end
+ setrange key offset value //从偏移量offset开始的子串设置为value

演示:

setrange key 0  'HHH' // hello  ===> HHHlo 
setrange key 5 ',world' // hello  ===> hello,world 

二进制位bit

把字符串当成bit数组处理

二进制位

命令

+ setbit key offset value //把指定的bit位设置为value(value只能是0或1)
+ getbit key offset //获取指定bit位的二进制的值
+ bitcount key [start end] //统计指定范围内bit位为1的数量

//对一个或多个二进制串执行按位运算,结果保存在destKey键中。操作包括并(and)、或(or)、异或(xor)和非(not)
+ bitop operation destKey key1 [key2 key3 ...] 

列表list

列表list

基本命令

+ lpush key value [value ...] //从左侧推入
+ rpush key value [value ...] //从右侧推入
+ lpop key //从左侧弹出
+ rpop key //从右侧弹出
+ lindex key offset //获取偏移量为offset的元素
+ lrange key start end //获取给定范围的所有元素,包括start和end
+ ltrim key start end //对列表进行修剪,只保留start到end之间的元素,包括start和end。

阻塞式(Block)及列表移动

+ blpop key1 [key2 key3 ... ]  timeout //从第一个非空列表中弹出左侧元素
+ brpop key1 [key2 key3 ... ]  timeout //从第一个非空列表中弹出右侧元素
+ rpoplpush sourceKey destKey //从sourceKey右侧弹出元素,推入destKey 左侧,并返回该元素
+ brpoplpush sourceKey destKey timeout //阻塞式rpoplpush,如果sourceKey为空则阻塞

集合set

没有排序,不重复。添加、删除、是否存在等操作(时间复杂度是 O(1) )

集合set

基本命令

+ sadd key item [item ...] //将元素添加到集合key中
+ srem key item [item ...] //从集合key中删除元素item 
+ sismember key item //判断元素item是否在集合key中
+ smembers key //获取集合key中的所有元素

//随机的返回一个或多个元素。count为正,则返回的元素不重复;count为负,则返回的元素可能会重复
+ srandmember key [count]
+ scard key //获取集合中元素的数量
+ spop key //随机的移除一个元素
+ smove sourceKey destKey item //把元素item从集合sourceKey移动到destKey中

运算操作

set 类型提供了多个 set 之间的聚合运算,如求交集、并集、补集,这些操作在 redis 内部完成,效率很高。

+ sdiff key1 [key2 key3 ...] //差集运算,返回存在第一个集合,但不存在于其他集合的元素
+ sdiffstore destKey key1 [key2 key3 ...] //把差集运算的结果存储到destKey 集合中

+ sinter key1 [key2 key3 ...] //交集运算,返回同时存在于所有集合中的元素
+ sinterstore destKey key1 [key2 key3 ...] //把交集运算的结果存储到destKey 集合中

+ sunion key1 [key2 key3 ...] //并集运算,返回至少存在于一个集合中的元素
+ sunionstore destKey key1 [key2 key3 ...] //把并集运算的结果存储到destKey 集合中

散列hash

散列hash

基本命令

+ hset hashKey subKey value //把键值对[subKey:value]添加到散列hashKey中
+ hget hashKey subKey //获取指定散列键的值
+ hmset hashKey subKey value [subKey value ...] //批量添加多个键
+ hmget hashKey subKey [subKey ...] //批量获取多个键
+ hdel hashKey subKey [subKey ...] //移除指定的散列键
+ hlen hashKey //返回散列中键值对的数量
+ hexists hashKey key //判断给定键是否存在于散列中
+ hkeys hashKey //获取散列 hashKey 中所有的键
+ hvals hashKey //获取散列 hashKey 中所有的值
+ hgetall hashKey //获取散列中所有的键值对

递增

+ hincr hashKey key amount //键key的值加上整数amount
+ hincrbyfloat hashKey key amount //键key的值加上浮点数amount

有序集合zset

存储结构与散列一样,都是键值对。
键被称为成员member,值被称为分值score,且必须为浮点数。

有序集合

基本命令

+ zadd key score member [score member ...] //添加成员
+ zrem key member [member  ...] //删除成员
+ zincrby key increment member //成员member的分值加上increment
+ zrank key member //返回成员member的排名
+ zscore key member //返回成员member的分值
+ zcard key //返回成员的数量
+ zcount key min max //返回分值介于min和max之间的成员数量

范围操作

+ zrange key start stop  [withscores] //返回排名介于start和stop之间的成员。如果指定withscores,则分值一起返回
+ zrangebyscore key min max [withscores] // 返回分值介于min和max之间的成员。
+ zremrangebyrank key start stop //移除排名介于start和stop之间的所有成员。
+ zremrangebyscore key min max //移除分值介于min和max之间的所有成员。

逆序操作

按照分值从大到小排列。

+ zrevrank key member //返回成员member的逆序排名
+ zrevrange key start stop  [withscores] //返回逆序排名介于start和stop之间的成员
+ zrevrangebyscore key min max [withscores] //返回分值介于min和max之间的成员,并按分值从大到小的顺序返回它们


这篇关于Redis常用命令的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程