NoSQL database case combat--Detailed explanation of Redis data types (String, Hash, List, Sets, Sorted-Sets)

Detailed explanation of Redis data types (String, Hash, List, Sets, Sorted-Sets)

Preface

This environment is based on the Centos 7.8 system to build a Redis learning environment. For
specific construction, please refer to Redis-5.0.9 Environment Deployment

As a memory-based database, Redis is also configured with many data types. For example: string (String), hash (Hash), list (list), set (sets) and ordered set (sorted sets) and other types. Next we will introduce the data types of Redis in detail.


One, String

The string type is the most basic data storage type in Redis. It is binary safe in Redis, which means that the type can accept data in any format, such as JPEG image data or lson object description information. In Redis, the maximum data length that a string type Value can hold is 512M .

Atomic single operation

# 判断指定字符串是否存在
192.168.5.5:6379> exists sk3
(integer) 0
# 向指定字符串插入字符
192.168.5.5:6379> append sk3 'hello world'
(integer) 11
# 向指定字符串追加字符
192.168.5.5:6379> append sk3 123
(integer) 14
# 返回字符串的长度
192.168.5.5:6379> strlen sk1
(integer) 11
# 覆盖原有的字符串
192.168.5.5:6379> set sk3 10
OK
# 自增1
192.168.5.5:6379> incr sk3
(integer) 11
# 自减1
192.168.5.5:6379> decr sk3
(integer) 10
# 自增指定的步长
192.168.5.5:6379> incrby sk3 10
(integer) 20
# 自减指定的步长
192.168.5.5:6379> decrby sk3 10
(integer) 10
# 查看字符串的内容
192.168.5.5:6379> get sk3
"10"

Atomic double operation

# 存储指定的值,返回原有的值
192.168.5.5:6379> getset sk3 50
"10"
192.168.5.5:6379> getset sk3 50
"50"
192.168.5.5:6379> getset sk4 80
(nil)
# 定义字符串是,指定字符串的生存时间
192.168.5.5:6379> setex sk4 10 8
OK
192.168.5.5:6379> get sk4
"8"
192.168.5.5:6379> get sk4
(nil)
# 查看字符串的生存时间(-1不过期,-2过期)
192.168.5.5:6379> ttl sk4
(integer) -2
192.168.5.5:6379> ttl sk1
(integer) -1
# 给没有值得字符串设置字符
192.168.5.12:6379> exists sk4
(integer) 0
192.168.5.12:6379> setnx sk4 'nihao'
(integer) 1
192.168.5.12:6379> setnx sk2 'hello'
(integer) 0
192.168.5.12:6379> get sk2
"12"
192.168.5.12:6379> get sk4
"nihao"
# 替换指定的字符串
192.168.5.12:6379> set sk1 'hello world'
OK
192.168.5.12:6379> setrange sk1 0 nihao
(integer) 11
192.168.5.12:6379> get sk1
"nihao world"
# 截取指定的字符串
192.168.5.12:6379> set sk1 '012345678'
OK
192.168.5.12:6379> getrange sk1 2 5
"2345"
# 设定多个字符串
192.168.5.12:6379> mset ck1 1 ck2 2
OK
# 查看多个字符串
192.168.5.12:6379> mget ck1 ck2
1) "1"
2) "2"
# 多个字符串不存在,再设置值
192.168.5.12:6379> msetnx ck3 3 ck4 4 ck5 5
(integer) 1
# 查看多个字符串的值
192.168.5.12:6379> mget ck3 ck4 ck5
1) "3"
2) "4"
3) "5"

Two, Hash

We can think of the Hashes type in Redis as a map container with String Key and String Value. So this type is very suitable for storing information about value objects. Such as Username, Password and Age, etc. If the Hash contains a few fields, then this type of data will only take up very little disk space. Each Hash can store 4294967295 key-value pairs.

Atomic single operation

# 查看哈希值是否存在
192.168.5.12:6379> hexists hk1 1
(integer) 0
# 设定哈希值
192.168.5.5:6379> hset hk1 f1 'a'
(integer) 1
192.168.5.5:6379> hset hk1 f2 'b'
(integer) 1
# 获取哈希值对应字段的值
192.168.5.5:6379> hget hk1 f1
"a"
192.168.5.5:6379> hget hk1 f2
"b"
# 查看指定哈希值是否存在
192.168.5.5:6379> hexists hk1 f1
(integer) 1
# 返回哈希值的字段数
192.168.5.5:6379> hlen hk1
(integer) 2
# 设定哈希值空字段的值
192.168.5.12:6379> hsetnx hk1 f1 'c'
(integer) 1
192.168.5.12:6379> hget hk1 f1
"c"
# 查看哈希值的长度
192.168.5.5:6379> hlen hk1
(integer) 3

Atomic double operation

# 一行设置多个字段的哈希值
192.168.5.5:6379> hmset hk2 f1 'a' f2 'b' f3 'c'
OK
# 批量查看哈希值
192.168.5.5:6379> hmget hk2 f1 f2 f3
1) "a"
2) "b"
3) "c"
# 查看哈希所有的值
192.168.5.5:6379> hvals hk2
1) "b"
2) "a"
3) "c"
# 自增指定的长度
192.168.5.5:6379> hincrby hk1 f1 10
(integer) 11
192.168.5.5:6379> hget hk1 f1
"11"
# 获取哈希值所有的键值对
192.168.5.5:6379> hgetall hk1
1) "f1"
2) "11"
3) "f2"
4) "2"
5) "f3"
6) "c"
# 获取所有的键
192.168.5.5:6379> hkeys hk1
1) "f1"
2) "f2"
3) "f3"

Three, List

In Redis, the List type is a linked list of strings. Like a normal linked list in a data structure, we can add new elements to its head (left) and tail (right). When inserting, if the key does not exist, Redis will create a new linked list for the key. In contrast, if all elements in the linked list are removed, the key will also be deleted from the database. The maximum number of elements that can be contained in the List is 4294967295.

# 列表中插入元素
 192.168.5.5:6379> lpush key_noe 1 2 3 4 5
(integer) 5
192.168.5.5:6379> rpush key_two a b c d e
(integer) 5
# 读取列表中的元素
192.168.5.5:6379> lrange key_noe 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
192.168.5.5:6379> lrange key_two 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
# 当key存在时,插入值
192.168.5.5:6379> lpushx key_noe a b
(integer) 12
# 查看列表长度
192.168.5.5:6379> llen key_two
(integer) 5
# 弹出栈底的元素
192.168.5.5:6379> lpop key_two
"a"
# 弹出栈底 的元素
192.168.5.5:6379> lpop key_two
"a"
# 按照指定的顺序和个数删除元素
192.168.5.12:6379> lpush kk1 a b c d a b c d a b c d
# 从头到尾删除
192.168.5.12:6379> lrem kk1 2 d
# 从未到头删除
192.168.5.12:6379> lrem kk1 -2 a
# 删除所有匹配到的元素
192.168.5.12:6379> lrem kk1 0 b
# 第一个值替换
192.168.5.5:6379> lset key_one 0 ab
OK
# 在d之前插入元素
192.168.5.12:6379> linsert kk2 before d xing
# 在d之后插入元素
192.168.5.12:6379> linsert kk2 after d ming
# 按照索引,取值
192.168.5.5:6379> lindex key_one 0
"ab"
# 按照索引删除元素,按照压栈顺序删除
192.168.5.5:6379> ltrim key_two 0 2
OK
# 插入元素
192.168.5.5:6379> rpush rk1 a b c d e
(integer) 5
# 读取元素
192.168.5.5:6379> lrange rk1 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"

Four, Sets

In Redis, we can regard the Set type as an unsorted character set. Like the List type, we can also perform operations such as adding, deleting, or judging whether an element exists on the data value of this type, and the Set can contain The maximum number of elements is 4294967295. If you add the same element multiple times, only one copy of the element will be kept in the Set.
Compared with the List type, the Set type still has a very important feature in function, that is, the aggregation calculation operation between multiple Sets is completed on the server side, such as unions, intersections, and differences. Since these operations are completed on the server side, the efficiency is extremely high, and it also saves a lot of network IO overhead.

Features of Sets

  • Disorder: The data in the collection is not distinguished in order.
  • Certainty: The number of data in the collection is certain.
  • Uniqueness: The data in the collection cannot be duplicated with each other.
# 插入数据到lk1
192.168.5.12:6379> sadd lk1 a b c d 
(integer) 4
# 查看集合lk1的数据
192.168.5.12:6379> smembers lk1
1) "d"
2) "b"
3) "a"
4) "c"
# 查看元素b是否在lk1中
192.168.5.12:6379> sismember lk1 b
(integer) 1
# 查看lk1元素的个数
192.168.5.12:6379> scard lk1
(integer) 4
# 随机显示lk1中的元素
192.168.5.12:6379> srandmember lk1
"c"
# 弹出lk1中的元素
192.168.5.12:6379> spop lk1
"d"
# 删除lk1中指定的元素
192.168.5.12:6379> srem lk1 b c
(integer) 2
# 移动元素
192.168.5.12:6379> smove lk1 lk12 a
(integer) 1
# 求差集
192.168.5.12:6379> sadd s1 a b c d e 
(integer) 5
192.168.5.12:6379> sadd s2 b
(integer) 1
192.168.5.12:6379> sadd s3 b c d 
(integer) 3
192.168.5.12:6379> sdiff s1 s2
1) "c"
2) "e"
3) "d"
4) "a"
192.168.5.12:6379> sdiff s3 s2
1) "c"
2) "d"
# 将差集插入s4中
192.168.5.12:6379> sdiffstore s4 s1 s2
(integer) 4
192.168.5.12:6379> smembers s4
1) "c"
2) "e"
3) "d"
4) "a"
# 求交集
192.168.5.12:6379> sinter s1 s2 s3
1) "b"
# 将交集插入s5中
192.168.5.12:6379> sinterstore s5 s1 s2 s3
(integer) 1
192.168.5.12:6379> smembers s5
1) "b"
# 求并集
192.168.5.12:6379> sunion s2 s3
1) "c"
2) "b"
3) "d"
# 将并集插入s6中
192.168.5.12:6379> sunionstore s6 s2 s3
(integer) 3
192.168.5.12:6379> smembers s6
1) "c"
2) "b"
3) "d"

五、Sorted-Sets

The members in Sorted-Sets must be unique, but scores can beRepeatedAnd alsoOrderlyof.
In fact, this feature of Redis is difficult to achieve in many other types of databases. In other words, it is very difficult to model in other databases to achieve the same efficiency as Redis at this point. of.

Zsets features

  • Uniqueness: The data in the collection is unique.
  • Order: The data in the collection is ordered.
  • Repeatability: The data in the collection can be repeated.
# 添加元素到zsets
192.168.5.12:6379> zadd zk1 1 'one' 2 'two' 3 'three'
(integer) 3
# 添加分数一致的成员
192.168.5.12:6379> zadd zk3 1 a 1 b 1 c
(integer) 3
192.168.5.12:6379> zrange zk3 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "1"
5) "c"
6) "1"
# 查看zsets中的元素个数
192.168.5.12:6379> zcard zk1
(integer) 3
# 返回zk1的键和值(按照分数从小到大排序)
192.168.5.12:6379> zrange zk1 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
# 返回zk1的索引(从零开始计数)
192.168.5.12:6379> zrank zk1 two
(integer) 1
# 返回满足score在1--2 之间的元素个数
192.168.5.12:6379> zcount zk1 1 2
(integer) 2
# 删除成员
192.168.5.12:6379> zrem zk1 three two
(integer) 2
# 查看成员对应的分数
192.168.5.12:6379> zscore zk1 one
"1"
# 成员分数加5
192.168.5.12:6379> zincrby zk1 5 one
"6"

Guess you like

Origin blog.csdn.net/XY0918ZWQ/article/details/112390342