Detailed explanation of Redis data types: hash type and zset type

Posted by cricher on Sat, 01 Jan 2022 19:26:58 +0100

1. hash data type and structure

1.1 introduction to hash types

Redis hash data structure is a key value pair (key value) set, which is a mapping table of field and value of string type. Redis itself is a key value database, so the hash data structure is equivalent to another layer of key value data in value. Therefore, the hash data structure in redis is particularly suitable for storing related objects, such as basic student information or user information.

1.2 common commands

1. hmset command
Description: used to set multiple field value pairs to the hash table at the same time. If a field already exists in the hash table, it will be overwritten. If the hash table does not exist, an empty hash table is created and the HMSET operation is performed.

127.0.0.1:6379> hmset users id 1 username zhangsan age 18 sex 1
OK
127.0.0.1:6379>

2. hmget command
Description: used to return the value corresponding to the field in the hash table. If the field does not exist, the nil value is returned.

127.0.0.1:6379> hmget users id username age
1) "1"
2) "zhangsan"
3) "18"
127.0.0.1:6379>

3. hset command
Description: assign the value corresponding to the field in the hash table. If the hash table does not exist, a new hash table is created and HSET operation is performed.

127.0.0.1:6379> hset users age 30
(integer) 0
127.0.0.1:6379> hset users sex 0
(integer) 0
127.0.0.1:6379> hmget users id username age sex
1) "1"
2) "zhangsan"
3) "30"
4) "0"
127.0.0.1:6379>

4. hvals command
Description: used to return the corresponding values of all field s in the hash table.

127.0.0.1:6379> hvals users
1) "1"
2) "zhangsan"
3) "30"
4) "0"
127.0.0.1:6379>

5. hdel command
Description: used to delete one or more fields in the specified hash table. Nonexistent fields will be ignored.

127.0.0.1:6379> hdel users sex
(integer) 1
127.0.0.1:6379> hvals users
1) "1"
2) "zhangsan"
3) "30"
127.0.0.1:6379>

6. hgetall command
Description: used to return all fields and values in the hash table.

127.0.0.1:6379> hgetall users
1) "id"
2) "1"
3) "username"
4) "zhangsan"
5) "age"
6) "30"
127.0.0.1:6379>

2. hash type application scenarios

2.1 shopping cart

The shopping cart function is mainly to add the goods to the shopping cart by clicking the goods. The front end will pass the commodity id and the data to be purchased to the back end. The back end will then complete the functions of adding the shopping cart, increasing or reducing the purchase quantity of the shopping cart, deleting or emptying the shopping cart through the parameters passed by the front end.

If we use redis, we can take the user id as the key, the commodity id as the field and the quantity of commodities as the value, which exactly constitute the three elements of the shopping cart.

2.2 storage objects

The structure of hash type (key, field, value) is similar to that of object (object id, attribute, value). It can also be used to store objects.

When introducing the application scenarios of string type, string + json is also a way to store objects. So, when storing objects, do you use string + json or hash?

The comparison of the two storage methods is shown in the following table:

When a property of an object needs to be modified frequently, string+json is not suitable because it is not flexible enough. Each modification needs to serialize and assign a value to the whole object. If hash type is used, a property can be modified separately without serialization or modifying the whole object. For example, attributes that may change frequently, such as commodity price, sales volume, number of concerns and number of evaluations, are suitable for storage in the hash type.

Of course, there is no problem storing infrequently changing attributes in hash types, such as product name, product description, launch date, etc. However, when an attribute of an object is not a basic type or string, complex serialization must be carried out manually by using hash type. For example, the label of a commodity is a list of label objects, The coupons available for goods are a list of coupon objects (as shown in the figure below). Even if coupons are used as the field and value wants to store the list of coupon objects, json should be used for serialization. In this way, the serialization work is too cumbersome. It is not as simple as directly storing commodity information in the form of string + json.

3. zset type and structure

3.1 introduction to Zset type

The redis ordered set is also a part of the set type, so it retains the feature that the elements in the set cannot be repeated. However, the difference is that the ordered set sets an additional score for each element, which is used as the basis for sorting.

Ordered sets can be sorted from small to large using scores. Although the members of an ordered set are unique, scores can be repeated. For example, in a class, the student number is unique, but the scores of each subject can be the same. redis can use an orderly collection to store student scores and quickly do the score ranking function.

3.2 common commands

1. zadd command
Description: adds one or more elements and their fractions to an ordered set. If the added element already exists, update the score value of the member, and then re insert it to locate its position. If the key does not exist, create a new ordered collection and insert it.

127.0.0.1:6379> zadd score 80 daxiong 90 xiaofu 100 panghu
(integer) 3

2. zcard command
Description: used to calculate the number of elements in an ordered set.

127.0.0.1:6379> zcard score
(integer) 3
127.0.0.1:6379>

3. zcount command
Description: used to calculate the number of members of a specified score interval in an ordered set.

127.0.0.1:6379> zcount score 80 90
(integer) 2
127.0.0.1:6379>

4. zrange command
Description: used to return members within a specified interval. The positions of members are sorted by increasing points (from small to large). The subscript parameters here all start from 0. A negative number means from the last member, and - 1 means the last member.

127.0.0.1:6379> zrange score 0 -1
1) "daxiong"
2) "xiaofu"
3) "panghu"
127.0.0.1:6379>

5. zrevrange command
Description: used to return members within the specified score range. The positions of members are sorted by decreasing fractional value (from large to small).

127.0.0.1:6379> zrevrange score 0 -1
1) "panghu"
2) "xiaofu"
3) "daxiong"
127.0.0.1:6379>

6. zrangebyscore command
Description: returns the member list of the specified score interval in the ordered set. The integrators are sorted by increasing score (from small to large).

127.0.0.1:6379> zrangebyscore score 80 90
1) "daxiong"
2) "xiaofu"
127.0.0.1:6379>

7. zrank command
Description: used to return the ranking of specified members in an ordered set. The members of the ordered set are arranged in the order of increasing fractional value (from small to large).

127.0.0.1:6379> zrank score panghu
(integer) 2
127.0.0.1:6379> zrank score xiaofu
(integer) 1
127.0.0.1:6379> zrank score daxiong
(integer) 0
127.0.0.1:6379>

8. zrem command
Description: used to remove one or more members from an ordered set. Nonexistent members will be ignored.

127.0.0.1:6379> zrem score xiaofu
(integer) 1
127.0.0.1:6379> zrange score 0 -1
1) "daxiong"
2) "panghu"
127.0.0.1:6379>

9. zremrangebyrank command
Description: used to remove all members in the specified rank interval from the ordered set.

127.0.0.1:6379> zremrangebyrank score 0 0
(integer) 1
127.0.0.1:6379> zrange score 0 -1
1) "panghu"
127.0.0.1:6379>

10. zscore command
Description: returns the score value of members in an ordered set. If the member element is not a member of the ordered set key, or the key does not exist, nil is returned.

127.0.0.1:6379> zadd score 95 tony
(integer) 1
127.0.0.1:6379> zscore score  tony
"95"
127.0.0.1:6379>

11. zadd command
Description: used to iterate over elements in an ordered set (including element members and element scores).

127.0.0.1:6379> zscan score 0 match t*
1) "0"
2) 1) "tony"
   2) "95"
127.0.0.1:6379>

4. zset type application scenario

Ranking List

Orderly collection of classic usage scenarios. For example, a video website needs to make a ranking list of videos uploaded by users. The maintenance of the list may be in many aspects: according to time, according to playback volume, according to the number of likes obtained, etc.
In order to better highlight the effect, redis uses string to record every time a commodity is browsed. Meanwhile, zset type will modify the scores of related commodities according to the records of string to ensure the real-time update of commodity ranking.

Topics: Database Redis