Redis中的五大数据类型

2023年 9月 21日 73.9k 0

系列文章目录

Redis(windows+Linux)安装及入门教程 - 掘金 (juejin.cn)

一、🎡前言

官方文档

Redis is an open source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

中文翻译:

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 geospatial(地理空间) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。

二、🥫Redis-key

Redis官方命令文档-Redis命令中心(Redis commands) -- Redis中国用户组(CRUG)

127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set key1 123
OK
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> get age #通过key获取value
"18"
127.0.0.1:6379> keys *
1) "age"
2) "key1"
127.0.0.1:6379> EXISTS age #判断key是否存在
(integer) 1
127.0.0.1:6379> EXISTS key1
(integer) 1
127.0.0.1:6379> EXISTS key2 #查询一个不存在的key
(integer) 0
127.0.0.1:6379> move age 1 #移动key到数据库1
(integer) 1
127.0.0.1:6379> keys *
1) "key1"
127.0.0.1:6379> select 1 #切换到数据库1
OK
127.0.0.1:6379[1]> keys *
1) "age"
127.0.0.1:6379> EXPIRE key1 10 #设置key的存活时间10s
(integer) 1
127.0.0.1:6379> ttl key1 #查看key的存活时间
(integer) 3
127.0.0.1:6379> ttl key1
(integer) 2
127.0.0.1:6379> ttl key1
(integer) 1
127.0.0.1:6379> ttl key1 #此时key已经过期
(integer) -2 
127.0.0.1:6379> keys * #再次查询,没有相关的key
(empty array)
127.0.0.1:6379> flushall #清空所有数据库
OK
127.0.0.1:6379> set test 1
OK
127.0.0.1:6379> set test2 2
OK
127.0.0.1:6379> keys *
1) "test2"
2) "test"
127.0.0.1:6379>type test #查看key对应的value是什么类型
string
127.0.0.1:6379> DEL test test2 #删除key,可以删一个也可以同时删除多个
(integer) 2
127.0.0.1:6379> keys *
(empty array)

三、🥓String(字符串)

String类型Redis官方文档-Redis Strings | Redis

默认情况下,Redis中单个string的最大容量是512MB

基础命令

  • SET stores a string value.
  • SETNX stores a string value only if the key doesn't already exist. Useful for implementing locks.
  • GET retrieves a string value.
  • MGET retrieves multiple string values in a single operation.

管理计数器

  • INCRBY atomically increments (and decrements when passing a negative number) counters stored at a given key.
  • Another command exists for floating point counters: INCRBYFLOAT.

命令演示

追加字符串以及获取字符串长度

127.0.0.1:6379> set key1 v1
OK
127.0.0.1:6379> APPEND key1 ",kkk" #追加字符串,如果当前key不存在,那便会创建一个key
(integer) 6
127.0.0.1:6379> get key1
"v1,kkk"

计数器

127.0.0.1:6379> set view 0
OK
127.0.0.1:6379> incr view #自增1
(integer) 1
127.0.0.1:6379> incr view
(integer) 2
127.0.0.1:6379> get view
"2"
127.0.0.1:6379> DECR view #自减1
(integer) 1
127.0.0.1:6379> DECR view
(integer) 0
127.0.0.1:6379> get view
"0"
127.0.0.1:6379> INCRBY view 10 #可以设置步长,指定增量
(integer) 10
127.0.0.1:6379> INCRBY view 10
(integer) 20
127.0.0.1:6379> get view
"20"
127.0.0.1:6379> DECRBY view 5 #指定减量
(integer) 15
127.0.0.1:6379> get view
"15"

字符串切片

127.0.0.1:6379> set key1 hello,world
OK
127.0.0.1:6379> GETRANGE key1 1 5 #截取字符串[1,5]
"ello,"

字符串替换

#替换
127.0.0.1:6379> set key2 aaaaaaaa
OK
127.0.0.1:6379> SETRANGE key2 3 b #替换指定位置上的字符串
(integer) 8
127.0.0.1:6379> get key2
"aaabaaaa"

设置存活时间

#setex(set with expire) #设置过期时间
#setnx(set if not expire) #不存在再设置(分布式锁中会常用)
127.0.0.1:6379> setex key3 30 hello #设置key3的值为hello且30秒后过期
OK
127.0.0.1:6379> TTL key3
(integer) 25
127.0.0.1:6379> get key3
"hello"
127.0.0.1:6379> setnx makey redis #如果makey不存在,创建makey
(integer) 1
127.0.0.1:6379> keys *
1) "key2"
2) "makey"
3) "key3"
4) "key1"
127.0.0.1:6379> ttl key3
(integer) -2
127.0.0.1:6379> setnx makey MongoDB #如果makey存在,创建失败
(integer) 0
127.0.0.1:6379> get makey
"redis"

批量操作

#批量设置
127.0.0.1:6379> MSET k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> keys *
1) "k2"
2) "k3"
3) "k1"
#批量取
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> MSETNX k1 v1 k4 v4 #msetnx是一个原子性操作,要么一起成功要么一起失败
(integer) 0
127.0.0.1:6379> keys *
1) "k2"
2) "k3"
3) "k1"

存取对象

# 对象
set user:1 {name:zhangsan,age:3} #设置一个user:1对象,值为json字符串来保存一个对象
127.0.0.1:6379> set user:1 {name:zhangsan,age:2}
OK
127.0.0.1:6379> mset user:1:name zhangsan user:1:age 2
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "2"

getset

127.0.0.1:6379> getset db redis #如果不存在值,则返回nil
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> getset db mongodb #如果存在值,则返回原来的值,并更新值
"redis"
127.0.0.1:6379> get db
"mongodb"

Sting类似的使用场景:value除了是字符串还可以是数字

四、🥖List(列表)

List类型Redis官方文档-Redis lists | Redis

基础命令

  • LPUSH adds a new element to the head of a list; RPUSH adds to the tail.
  • LPOP removes and returns an element from the head of a list; RPOP does the same but from the tails of a list.
  • LLEN returns the length of a list.
  • LMOVE atomically moves elements from one list to another.
  • LTRIM reduces a list to the specified range of elements.

阻塞命令

  • BLPOP removes and returns an element from the head of a list. If the list is empty, the command blocks until an element becomes available or until the specified timeout is reached.
  • BLMOVE atomically moves elements from a source list to a target list. If the source list is empty, the command will block until a new element becomes available.

在redis中,可以将list用成栈、队列、阻塞队列

命令演示

插值操作

127.0.0.1:6379> LPUSH list 1 #将一个值或者多个值插入列表头部
(integer) 1
127.0.0.1:6379> LPUSH list 2
(integer) 2
127.0.0.1:6379> LPUSH list 3
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1 #获取列表全部数据
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> LRANGE list 0 1 #通过区间来获取特定的值
1) "3"
2) "2"
127.0.0.1:6379> RPUSH list last #将一个值插入列表的尾部
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "3"
2) "2"
3) "1"
4) "last"

移除操作

#LPOP左移除
#RPOP右移除
127.0.0.1:6379> LPOP list #移除list左边的元素
"3"
127.0.0.1:6379> RPOP list #移除list右边的元素	
"last"
127.0.0.1:6379> LRANGE list 0 -1
1) "2"
2) "1"

通过下标获取元素

#lindex 通过下标获取数组的元素
127.0.0.1:6379> lindex list 0
"2"
127.0.0.1:6379> lindex list 1
"1"

获取长度

#通过llen获取list的长度
127.0.0.1:6379> LLEN list
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "2"
2) "1"

移除指定值

# lrem移除指定值
127.0.0.1:6379> LRANGE list 0 -1
1) "4"
2) "2"
3) "5"
4) "5"
5) "3"
6) "2"
7) "1"
127.0.0.1:6379> LREM list 1 1 #移除指定的一个值,一个1被移除
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "4"
2) "2"
3) "5"
4) "5"
5) "3"
6) "2"
127.0.0.1:6379> LREM list 2 5 #移除指定的两个值,两个5被移除
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "4"
2) "2"
3) "3"
4) "2"

截断操作

127.0.0.1:6379> LPUSH mylist hello0
(integer) 1
127.0.0.1:6379> LPUSH mylist hello1
(integer) 2
127.0.0.1:6379> LPUSH mylist hello2
(integer) 3
127.0.0.1:6379> LPUSH mylist hello3
(integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello3"
2) "hello2"
3) "hello1"
4) "hello0"
127.0.0.1:6379> LTRIM mylist 0 1
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello3"
2) "hello2"

RPOPLPUSH命令

  • 将列表 source 中的最后一个元素(尾元素)弹出,并返回给客户端。
  • source 弹出的元素插入到列表 destination ,作为 destination 列表的头元素。
127.0.0.1:6379> RPUSH mylist hello0
(integer) 1
127.0.0.1:6379> RPUSH mylist hello1
(integer) 2
127.0.0.1:6379> RPUSH mylist hello2
(integer) 3
127.0.0.1:6379> RPUSH mylist hello3
(integer) 4
127.0.0.1:6379> RPOPLPUSH mylist otherlist #移除列表最后一个元素,将它移动到新的列表中
"hello3"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello0"
2) "hello1"
3) "hello2"
127.0.0.1:6379> LRANGE otherlist 0 -1
1) "hello3"

将列表中指定下标的值替换

#LSET使用的前提是,key存在
127.0.0.1:6379> LPUSH list value0
(integer) 1
127.0.0.1:6379> LSET list 0 replace_value #如果存在,更新当前下标的值,不存在则报错
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "replace_value"

指定位置插入操作

#LINSERT在某个元素的前面或者后面插入值
127.0.0.1:6379> RPUSH list 0
(integer) 1
127.0.0.1:6379> RPUSH list 1
(integer) 2
127.0.0.1:6379> RPUSH list 2
(integer) 3
127.0.0.1:6379> LINSERT list before 2 value #在2之前插入value
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "0"
2) "1"
3) "value"
4) "2"
127.0.0.1:6379> LINSERT list after value val #在value后面插入val
(integer) 5
127.0.0.1:6379> LRANGE list 0 -1
1) "0"
2) "1"
3) "value"
4) "val"
5) "2"

五、🍱 Set(集合)

Set类型Redis官方文档-Redis sets | Redis

基础命令

  • SADD adds a new member to a set.
  • SREM removes the specified member from the set.
  • SISMEMBER tests a string for set membership.
  • SINTER returns the set of members that two or more sets have in common (i.e., the intersection).
  • SCARD returns the size (a.k.a. cardinality) of a set.

命令演示

添加值到set中并查看

127.0.0.1:6379> SADD myset hello #添加值
(integer) 1
127.0.0.1:6379> SADD myset test
(integer) 1
127.0.0.1:6379> 
127.0.0.1:6379> SADD myset value
(integer) 1
127.0.0.1:6379> SMEMBERS myset #查看指定set中所有的值
1) "hello"
2) "test"
3) "value"

查询值是否存在

127.0.0.1:6379> SISMEMBER myset hello #值存在
(integer) 1
127.0.0.1:6379> SISMEMBER myset hello1 #值不存在
(integer) 0

获取set中的元素的个数

127.0.0.1:6379> SCARD myset
(integer) 3

移除set中的元素

127.0.0.1:6379> SMEMBERS myset
1) "hello"
2) "test"
3) "value"
127.0.0.1:6379> SREM myset hello #移除set中的指定元素
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "test"
2) "value"

随机筛选

127.0.0.1:6379> SRANDMEMBER myset #随机抽选出一个元素
"test"
127.0.0.1:6379> SRANDMEMBER myset
"test"
127.0.0.1:6379> SRANDMEMBER myset
"value"
127.0.0.1:6379> SADD myset 1
(integer) 1
127.0.0.1:6379> SADD myset 2
(integer) 1
127.0.0.1:6379> SADD myset 3
(integer) 1
127.0.0.1:6379> SRANDMEMBER myset 2 #指定数量随机抽取
1) "test"
2) "1"

随机删除key

127.0.0.1:6379> SPOP myset #随机删除一个
"3"
127.0.0.1:6379> SPOP myset
"test"
127.0.0.1:6379> SPOP myset 2 #指定数量随机删除2个
1) "value"
2) "2"
127.0.0.1:6379> SMEMBERS myset
1) "1"

指定值移动到另外set中

127.0.0.1:6379> SADD set1 1
(integer) 1
127.0.0.1:6379> SADD set1 2
(integer) 1
127.0.0.1:6379> SADD set1 3
(integer) 1
127.0.0.1:6379> SADD set1 4
(integer) 1
127.0.0.1:6379> SADD set2 a
(integer) 1
127.0.0.1:6379> SADD set2 b
(integer) 1
127.0.0.1:6379> SADD set2 c
(integer) 1
127.0.0.1:6379> SADD set2 d
(integer) 1
127.0.0.1:6379> SMOVE set1 set2 1 #将指定的值移动从一个set移动到另一个set中
(integer) 1
127.0.0.1:6379> SMEMBERS set1
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> SMEMBERS set2
1) "a"
2) "b"
3) "c"
4) "d"
5) "1"

求差集,交集,并集

127.0.0.1:6379> SADD set1 a
(integer) 1
127.0.0.1:6379> SADD set1 b
(integer) 1
127.0.0.1:6379> SADD set1 c
(integer) 1
127.0.0.1:6379> SADD set2 c
(integer) 1
127.0.0.1:6379> SADD set2 d
(integer) 1
127.0.0.1:6379> SADD set2 e
(integer) 1
127.0.0.1:6379> SDIFF set1 set2 #差集
1) "a"
2) "b"
127.0.0.1:6379> SINTER set1 set2 #交集
1) "c"
127.0.0.1:6379> SUNION set1 set2 #并集
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"

六、🍖Hash(哈希)

Map集合,key-map时候,这个值是一个map集合,本质上和String类型没有太大区别,还是一个key-value

Hash类型Redis官方文档-Redis hashes | Redis

基础命令

  • HSET sets the value of one or more fields on a hash.
  • HGET returns the value at a given field.
  • HMGET returns the values at one or more given fields.
  • HINCRBY increments the value at a given field by the integer provided.

命令演示

添加以及批量添加

127.0.0.1:6379> HSET hash1 name zhangsan #设置键,字段,值
(integer) 1
127.0.0.1:6379> HGET hash1 name #获取一个字段值
"zhangsan"
127.0.0.1:6379> HMSET hash1 name zhangsan age 18 type person #批量添加字段和值
OK

查询

127.0.0.1:6379> HmGET hash1 name age type #获取多个字段值
1) "zhangsan"
2) "18"
3) "person"
127.0.0.1:6379> HGETALL hash1 #查询全部
1) "name"
2) "zhangsan"
3) "age"
4) "18"
5) "type"
6) "person"

删除

127.0.0.1:6379> HDEL hash1 name #删除hash指定的key字段
(integer) 1
127.0.0.1:6379> HGETALL hash1
1) "age"
2) "18"
3) "type"
4) "person"

获取hash的长度

127.0.0.1:6379> hlen hash1 #获取键值对的数量
(integer) 2
127.0.0.1:6379> hgetall hash1
1) "age"
2) "18"
3) "type"
4) "person"

判断键值对是否存在

#HEXISTS key field 判断key中的字段是否存在
127.0.0.1:6379> hgetall hash1
1) "age"
2) "18"
3) "type"
4) "person"
127.0.0.1:6379> HEXISTS hash1 age 
(integer) 1
127.0.0.1:6379> HEXISTS hash1 name
(integer) 0

获取所有的field和value

127.0.0.1:6379> HKEYS hash1
1) "age"
2) "type"
127.0.0.1:6379> HVALS hash1
1) "18"
2) "person"

指定字段的值自增自减

127.0.0.1:6379> HSET hash1 field 5
(integer) 1
127.0.0.1:6379> HINCRBY hash1 field 1
(integer) 6
127.0.0.1:6379> HINCRBY hash1 field 2
(integer) 8
127.0.0.1:6379> HINCRBY hash1 field -1 #等同于自减
(integer) 7
127.0.0.1:6379> HGET hash1 field
"7"

hsetnx命令

127.0.0.1:6379> HSETNX hash1 field2 hello #存在则创建成功
(integer) 1
127.0.0.1:6379> HSETNX hash1 field2 hello #不存在则创建失败
(integer) 0

七、🍳 Zset(有序集合)

ZsetReids官方文档-Redis sorted sets | Redis

基础命令

  • ZADD adds a new member and associated score to a sorted set. If the member already exists, the score is updated.
  • ZRANGE returns members of a sorted set, sorted within a given range.
  • ZRANK returns the rank of the provided member, assuming the sorted is in ascending order.
  • ZREVRANK returns the rank of the provided member, assuming the sorted set is in descending order.

命令演示

添加元素

127.0.0.1:6379> ZADD myset 1 one #添加一个值
(integer) 1
127.0.0.1:6379> ZADD myset 2 two
(integer) 1
127.0.0.1:6379> ZADD myset 3 three #添加多个值
(integer) 1
127.0.0.1:6379> ZRANGE myset 0 -1
1) "one"
2) "two"
3) "three"

排序

127.0.0.1:6379> ZADD salary 2500 zhangsan #添加三个用户
(integer) 1
127.0.0.1:6379> ZADD salary 5000 lisi
(integer) 1
127.0.0.1:6379> ZADD salary 500 wangwu
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf #显示全部用户,从小到大排序,inf为无穷
1) "wangwu"
2) "zhangsan"
3) "lisi"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores #显示所有的用户并附带值
1) "wangwu"
2) "500"
3) "zhangsan"
4) "2500"
5) "lisi"
6) "5000"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 3000 withscores #显示小于3000的值
1) "wangwu"
2) "500"
3) "zhangsan"
4) "2500"
127.0.0.1:6379> ZREVRANGE salary 0 -1 #从大到小排序
1) "zhangsan"
2) "wangwu"

移除元素

127.0.0.1:6379> ZRANGE salary 0 -1
1) "wangwu"
2) "zhangsan"
3) "lisi"
127.0.0.1:6379> ZREM salary lisi
(integer) 1
127.0.0.1:6379> ZRANGE salary 0 -1
1) "wangwu"
2) "zhangsan"

获取集合中元素的个数

127.0.0.1:6379> zcard salary
(integer) 2

查询zset中区间内有多少元素

127.0.0.1:6379> ZADD myset 1 val1
(integer) 1
127.0.0.1:6379> ZADD myset 2 val2
(integer) 1
127.0.0.1:6379> ZADD myset 3 val3
(integer) 1
127.0.0.1:6379> ZADD myset 4 val4
(integer) 1
127.0.0.1:6379> ZADD myset 5 val5
(integer) 1
127.0.0.1:6379> ZCOUNT myset 0 3 #获取0到3间有多少成员
(integer) 3
127.0.0.1:6379> ZCOUNT myset 1 4
(integer) 4

参考资料

【狂神说Java】Redis最新超详细版教程通俗易懂_哔哩哔哩_bilibili

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论