Redis五种数据类型

2023-03-06 15:13:36 浏览数 (1)

String

键值对

12345678910

> set name codehole OK > get name "codehole"> exists name (integer) 1 > del name (integer) 1 > get name (nil)

批量键值对

12345678910111213

> set name1 codehole OK > set name2 holycoder OK > mget name1 name2 name3 # 返回一个列表1) "codehole" 2) "holycoder" 3) (nil) > mset name1 boy name2 girl name3 unknown > mget name1 name2 name3 1) "boy" 2) "girl" 3) "unknown"

过期时间

12345678910111213

> set name codehole > get name "codehole" > expire name 5 # 5s 后过期... # wait for 5s > get name (nil) > setex name 5 codehole # 5s 后过期,等价于 set expire> get name "codehole" ... # wait for 5s > get name (nil)

如果重新设置了值,则过期时间会消失

12345678910

> set codehole yoyo OK > expire codehole 600 (integer) 1 > ttl codehole (integer) 597 > set codehole yoyo OK > ttl codehole (integer) -1

如果不存在则创建

12345678

> setnx name codehole # 如果 name 不存在就执行 set 创建(integer) 1 > get name "codehole" > setnx name holycoder (integer) 0 # 因为 name 已经存在,所以 set 创建不成功> get name "codehole" # 没有改变

计数

123456789101112

> set age 30 OK > incr age (integer) 31 > incrby age 5 (integer) 36 > incrby age -5 (integer) 31 > set codehole 9223372036854775807 # Long.Max OK > incr codehole(error) ERR increment or decrement would overflow

List

模拟队列:右边进左边出

123456789101112

> rpush books python java golang (integer) 3 > llen books (integer) 3 > lpop books "python" > lpop books "java" > lpop books "golang" > lpop books (nil)

模拟栈:右边进左边出

12345678910

> rpush books python java golang (integer) 3 > rpop books "golang" > rpop books "java" > rpop books "python" > rpop books(nil)

索引操作,由于list是链表数据结构而非数组,所以时间复杂度是O(n)

1234567891011121314151617

> rpush books python java golang (integer) 3 > lindex books 1 # O(n) 慎用"java" > lrange books 0 -1 # 获取所有元素,O(n) 慎用1) "python" 2) "java" 3) "golang" > ltrim books 1 -1 # 只保留指定区间的元素,O(n) 慎用OK > lrange books 0 -1 1) "java" 2) "golang" > ltrim books 1 0 # 这其实是清空了整个列表,因为区间范围长度为负OK > llen books (integer) 0

Hash

1234567891011121314151617181920212223

> hset books java "think in java" # 命令行的字符串如果包含空格,要用引号括起来(integer) 1 > hset books golang "concurrency in go" (integer) 1 > hset books python "python cookbook" (integer) 1 > hgetall books # entries(),key 和 value 间隔出现1) "java" 2) "think in java" 3) "golang" 4) "concurrency in go" 5) "python" 6) "python cookbook" > hlen books (integer) 3 > hget books java "think in java" > hset books golang "learning go programming" # 因为是更新操作,所以返回0(integer) 0> hget books golang"learning go programming"> hdel books golang # 删除指定键(integer) 1

批量操作

12

> hmset books java "effective java" python "learning python" golang "modern golang programming" # 批量 set OK

计数

12

> hincrby user-laoqian age 1 # 增加1(integer) 30

Set

set相当于没有值的字典,它能保证键不重复

12345678910111213141516171819202122

> sadd books python (integer) 1 > sadd books python # 重复(integer) 0 > sadd books java golang (integer) 2 > smembers books # 注意顺序,和插入的并不一致,因为 set 是无序的1) "java" 2) "python" 3) "golang" > sismember books java # 查询某个 value 是否存在,相当于 contains(o)(integer) 1 > sismember books rust (integer) 0 > scard books # 获取长度相当于 count()(integer) 3 > spop books # 弹出一个"java"> srem books python # 删除指定元素(integer) 1> srem books python golang # 删除多个指定元素(integer) 2

Sort Set

排序列表,可以为每个键指定一个分值,按分值排序

12345678910111213141516171819202122232425262728293031323334

> zadd books 9.0 "think in java" (integer) 1 > zadd books 8.9 "java concurrency" (integer) 1 > zadd books 8.6 "java cookbook" (integer) 1 > zrange books 0 -1 # 按 score 排序列出,参数区间为排名范围1) "java cookbook" 2) "java concurrency" 3) "think in java" > zrevrange books 0 -1 # 按 score 逆序列出,参数区间为排名范围1) "think in java" 2) "java concurrency" 3) "java cookbook" > zcard books # 相当于 count()(integer) 3> zscore books "java concurrency" # 获取指定 value 的 score"8.9000000000000004" # 内部 score 使用 double 类型进行存储,所以存在小数点精度问题> zrank books "java concurrency" # 排名(integer) 1 > zrangebyscore books 0 8.91 # 根据分值区间遍历 zset1) "java cookbook" 2) "java concurrency" > zrangebyscore books -inf 8.91 withscores # 根据分值区间 (-∞, 8.91] 遍历 zset,同时返回分值。inf 代表 infinite,无穷大的意思。1) "java cookbook" 2) "8.5999999999999996" 3) "java concurrency" 4) "8.9000000000000004" > zrem books "java concurrency" # 删除 value(integer) 1 > zrange books 0 -1 1) "java cookbook" 2) "think in java"

更多命令可参考:https://www.redis.net.cn/order/

0 人点赞