Redis (3)--Introduction to redis data structure

Preface
As mentioned in the introduction of redis above, there are many types of redis data structures, which can be applied to many scenarios, such as lottery system, sharing rewards, shopping cart management, public friend recommendation, etc. So specifically, what data structures does redis have, and what are the different characteristics of different data structures?

Diagram redis data structure

insert image description here

The common data structures of Redis are:

  1. String type, a typical key-value pair, query the value of value by key. Common functions include set, get, setex, setnx, mset, etc., which are often used to store data that does not require complex processing and has a small amount of data
  2. The Hash type, a typical object type, can be compared to a class. After passing the key (which can be understood as the class name), the value (which can be understood as the value of the member variable) is obtained through the field (which can be understood as the member variable name). Common functions include hset, hget, hgetall, hincrby, hlen, hkeys, hmget, etc. Often used to store structured data
  3. Bitmap type, a typical bit array, the number of data indicates the size of the number. Such as 1- 1 2- 10 3-100. It is often used in situations where large amounts of data need to be processed, such as Bloom filters.
  4. List type, typical stack (stack) and queue (queue) types, common functions include blpop, brpop, linsert, etc. Often used for data that needs to be queued.
  5. Set type, a typical collection type, is used to store related data, and then facilitate the processing of collection data. zset is an ordered set. There are many collection methods, which will be introduced later

Common applications:

  1. Lottery system: (the data structure is a set)
    a) We can use Sadd key member to put the product id to be drawn into the goods set (set) and put the users who are eligible for the lottery into another set members through this method .
    b) After that, before the lottery draw, to judge whether the current user is in the collection, just use the Sismember members member method to judge whether the member is in the members collection
    . The user selects randomly by setting the weight (weight) or the number (count), and then associates the prize with the selected user. [Note: This may generate duplicate user records]
    Improvement plan: use spop key count to avoid duplication
  2. Shopping cart: (data structure is hash)
    a) Store the items in each shopping cart as objects in redis, the key is the user shopping cart id or user id, the field is the item id, and the value is the number of items. Use the HMSET key [field1 value1 field2 value2] method to store the product in redis
    b) Use the HGETALL key to get all product information and purchase quantity.
  3. Bloom filter, using bitmap as data structure. See the follow-up article for details.
    4) For public friend recommendation, use the set set to find the intersection of sets through SINTERCARD key1 key2

more links

Redis installation https://blog.csdn.net/qq_31236027/article/details/121879741
redis introduction https://blog.csdn.net/qq_31236027/article/details/121879604

Guess you like

Origin blog.csdn.net/qq_31236027/article/details/121894713