Basic data types and basic application scenarios of redis

Time:2021-3-4

Basic application scenarios of redis

  1. Get the winning user ID. after it pops up randomly, the set does not exist
  2. Store the winning user ID in the activity to ensure that the same user won’t win twice [set]
  3. Store the fan list, value is the user ID of the fan, score is the follow time [Zset]
  4. Store the student’s score, value is the student’s ID, score is the test score [Zset]
  5. Record the number of likes, comments, and hits [hash]
  6. Record the user’s post ID list, so as to quickly display the user’s post list [Zset]
  7. Record the title, abstract, author and cover information of the post, which is used for list display page [hash]
  8. Record the like ID list and comment ID list of posts, used to display and de count [Zset]
  9. Cache recent hot posts to reduce database pressure [hash]
  10. Record hot list post ID list, total hot list and classified hot list [Zset]
  11. The relationship between collection and post [Zset]
  12. Cache user’s historical behavior and filter malicious behavior [Zset, hash]

Basic data types of redis

  1. Create value pair: it is equivalent to the key and value of the dictionary, and supports simple operation of adding, deleting, modifying and querying.

    1. set/get/exists/del
    2. Mset / mget = > batch key value pair
    3. Expiration = > set expiration time
    4. Setex = > 5 seconds, set + expire
    5. Setnx = > create if it doesn’t exist, and create if it does not succeed
    6. Incr / incrby = > value is an integer, which can be auto incremented and has a range.
    7. Get all the keys: keys*
    8. Clear all data: flushall
    127.0.0.1:6379 > single key value pair
    127.0.0.1:6379 > ########################
    127.0.0.1:6379> set name laowen 
    OK
    127.0.0.1:6379 > get name # get
    "laowen"
    127.0.0.1:6379 > exists name #
    (integer) 1
    127.0.0.1:6379 > del name # delete
    (integer) 1
    127.0.0.1:6379 > get name # get
    (nil)
    127.0.0.1:6379 > set name1 boy # setting
    OK
    127.0.0.1:6379 > set Name2 girl #
    OK
    127.0.0.1:6379 > mget name1 Name2 ා put back a list
    1) "boy"
    2) "girl"
    127.0.0.1:6379 > batch add multiple
    127.0.0.1:6379> mset name1 boy name2 girl name3 unknow
    OK
    127.0.0.1:6379> mget name1 name2 name3
    1) "boy"
    2) "girl"
    3) "unknow"
    127.0.0.1:6379 > set expiration time
    127.0.0.1:6379> set name a
    OK
    127.0.0.1:6379> get name
    "a"
    127.0.0.1:6379 > expiration name 5 # 5 seconds later
    (integer) 1
    127.0.0.1:6379 > get name # less than 5 seconds
    "a"
    127.0.0.1:6379 > get name # 5 seconds later
    (nil)
    127.0.0.1:6379 > ####5 seconds later, set + expire 
    127.0.0.1:6379> setex name 5  aa
    OK
    127.0.0.1:6379> get name
    "aa"
    127.0.0.1:6379> get name
    (nil)
    127.0.0.1:6379 > #########################
    127.0.0.1:6379 > setnx name a # create if it doesn't exist
    (integer) 1
    127.0.0.1:6379> get names
    "a"
    127.0.0.1:6379 > setnx name B # if it exists, it will not be created successfully
    (integer) 0
    127.0.0.1:6379 > get name #
    "a"
    127.0.0.1:6379 > count
    127.0.0.1:6379> set age 20
    OK
    127.0.0.1:6379> get age 
    "20"
    127.0.0.1:6379> incr age
    (integer) 21
    127.0.0.1:6379> get age 
    "21"
    127.0.0.1:6379> incrby age 5
    (integer) 26
    127.0.0.1:6379> get age 
    "26"
  2. List list, note that it is a linked list, not an array.

    Redis’s list structure is often used as an asynchronous queue. Queue is a first in, first out data structure, which is often used in message queue and asynchronous logic processing to ensure the access sequence of elements.

    When the last element in the list pops up, the data structure is automatically deleted and the memory is recycled.

    1. [queue: right in left out]

      1. Rpush tail appending
      2. Get the length of
      3. Delete the lpop header
      127.0.0.1:6379 >? Add a books queue
      127.0.0.1:6379> rpush books php java net golang 
      (integer) 4
      127.0.0.1:6379 > continue to add content at the end of books queue
      127.0.0.1:6379> rpush books c
      (integer) 5
      127.0.0.1:6379 > continue to add content at the end of books queue
      127.0.0.1:6379> rpush books python
      (integer) 6
      127.0.0.1:6379 > continue to add content at the end of books queue
      127.0.0.1:6379> rpush books vue
      (integer) 7
      127.0.0.1:6379 > ා get the length of books queue
      127.0.0.1:6379> llen books
      (integer) 7
      127.0.0.1:6379 > pop up the first content of the books queue
      127.0.0.1:6379> lpop books
      "php"
      127.0.0.1:6379 > pop up the first content of the books queue
      127.0.0.1:6379> lpop books
      "java"
      127.0.0.1:6379 > pop up the first content of the books queue
      127.0.0.1:6379> lpop books
      "net"
      127.0.0.1:6379 > pop up the first content of the books queue
      127.0.0.1:6379> lpop books
      "golang"
      127.0.0.1:6379 > pop up the first content of the books queue
      127.0.0.1:6379> lpop books
      "c"
      127.0.0.1:6379 > pop up the first content of the books queue
      127.0.0.1:6379> lpop books
      "python"
      127.0.0.1:6379 > pop up the first content of the books queue
      127.0.0.1:6379> lpop books
      "vue"
      127.0.0.1:6379 > pop up the first content of the books queue
      127.0.0.1:6379> lpop books
      (nil)
      127.0.0.1:6379>
    2. [stack: right in and right out]

      1. Rpush tail appending
      2. Get the length of
      3. Remove rpop from tail
      127.0.0.1:6379 >? Add a books stack
      127.0.0.1:6379> rpush books php java net golang 
      (integer) 4
      127.0.0.1:6379 > get the length of books stack
      127.0.0.1:6379> llen books
      (integer) 4
      127.0.0.1:6379 > pop up the content at the end of the stack
      127.0.0.1:6379> rpop books
      "golang"
      127.0.0.1:6379 > pop up the content at the end of the stack
      127.0.0.1:6379> rpop books
      "net"
      127.0.0.1:6379 > pop up the content at the end of the stack
      127.0.0.1:6379> rpop books
      "java"
      127.0.0.1:6379 > pop up the content at the end of the stack
      127.0.0.1:6379> rpop books
      "php"
      127.0.0.1:6379 > pop up the content at the end of the stack
      127.0.0.1:6379> rpop books
      (nil)
      127.0.0.1:6379>
    3. [slow operation]

    4. [quick list]

  3. Hash dictionary

    Similar to dictionary, a key corresponds to a value. It is suitable for storing key as title and value as content. When hash removes the last element, the data structure is automatically deleted and the memory is recycled.

    1. Hset key filed value
    2. Get key file
    3. Hgetall key to get all
    4. HDEL key filed delete single
    5. Getting length of HLEN key
    6. Hmset key file value batch setting
    127.0.0.1:6379 > ##########################
    127.0.0.1:6379> hset name wang wangzong
    (integer) 1
    127.0.0.1:6379> hset name wen wenzong
    (integer) 1
    127.0.0.1:6379> hset name zhang zhangzong
    (integer) 1
    127.0.0.1:6379 > get all
    127.0.0.1:6379> hgetall name
    1) "wang"
    2) "wangzong"
    3) "wen"
    4) "wenzong"
    5) "zhang"
    6) "zhangzong"
    127.0.0.1:6379 > get length
    127.0.0.1:6379> hlen name
    (integer) 3
    127.0.0.1:6379 > > get single
    127.0.0.1:6379> hget name wen
    "wenzong"
    127.0.0.1:6379 > ා update single, return 0 successfully
    127.0.0.1:6379> hset name wen wenzongbin
    (integer) 0
    127.0.0.1:6379> hget name wen
    "wenzongbin"
    127.0.0.1:6379 > ා update single, return 0 successfully
    127.0.0.1:6379> hset name wen xiaowen
    (integer) 0
    127.0.0.1:6379> hgetall name
    1) "wang"
    2) "wangzong"
    3) "wen"
    4) "xiaowen"
    5) "zhang"
    6) "zhangzong"
    127.0.0.1:6379 > batch set
    127.0.0.1:6379> hmset name zhao zhaozong li lizong fei feizong
    OK
    127.0.0.1:6379> hgetall name
     1) "wang"
     2) "wangzong"
     3) "wen"
     4) "xiaowen"
     5) "zhang"
     6) "zhangzong"
     7) "zhao"
     8) "zhaozong"
     9) "li"
    10) "lizong"
    11) "fei"
    12) "feizong"
    127.0.0.1:6379> hget name wen
    "xiaowen"
    127.0.0.1:6379 > delete single
    127.0.0.1:6379> hdel name wen
    (integer) 1
    127.0.0.1:6379> hget name wen
    (nil)
    127.0.0.1:6379>
  4. Set set

    There are key value pairs in the redis collection, which are unordered and unique.

    1. Sadd key value = > Add
    2. Smembers key = > get all
    3. Smembers key value = > check whether a value exists
    4. Scar key = > get length
    5. Pop key = > pop up randomly
    127.0.0.1:6379 > ා add one, does not exist, returns 1
    127.0.0.1:6379> sadd num 100 
    (integer) 1
    127.0.0.1:6379 > ා add one, exist, return 0
    127.0.0.1:6379> sadd num 100	
    (integer) 0
    127.0.0.1:6379 > > add multiple
    127.0.0.1:6379> sadd num 99 89 
    (integer) 2
    127.0.0.1:6379 > get all 
    127.0.0.1:6379 > #, which is inconsistent with the order of insertion, because set is unordered
    127.0.0.1:6379> smembers num  
    1) "89"
    2) "99"
    3) "100"
    127.0.0.1:6379 > # query whether a value exists and return 1
    127.0.0.1:6379> sismember num 100 
    (integer) 1
    127.0.0.1:6379 > ා query whether a value exists or not, return 0
    127.0.0.1:6379> sismember num 90 
    (integer) 0
    127.0.0.1:6379 > scale num # get length
    (integer) 3
    127.0.0.1:6379 > pop num
    "100"
    127.0.0.1:6379 > pop num
    "89"
    127.0.0.1:6379 > pop num
    "99"
    127.0.0.1:6379> spop num
    (nil)
  5. Zset has a sequence table

    1. Zadd key score value = > Add
    2. Zrange key range = > listed in positive order of score, and the parameter is ranking range
    3. Zrevrange key range = > listed in reverse order of score, and the parameter is ranking range
    4. Zcard key = > get length, equivalent to count ()
    5. Zscore key value = > gets the score of the specified value
    6. Zrank key value = > gets the ranking of the specified value
    7. Zrangebyscore key range = > value according to score range
    8. Zrem key value = > delete the score of the specified value
    127.0.0.1:6379 > ා add the sequence table zadd key score value
    127.0.0.1:6379 > score
    127.0.0.1:6379> ## key:fan
    127.0.0.1:6379> ## value:wen
    127.0.0.1:6379> zadd fan 100 wen
    (integer) 1
    127.0.0.1:6379> zadd fan 99 zhang
    (integer) 1
    127.0.0.1:6379> zadd fan 101 li
    (integer) 1
    127.0.0.1:6379> zadd fan 98 zhao
    (integer) 1
    127.0.0.1:6379> zadd fan 97 jin
    (integer) 1
    127.0.0.1:6379 > # zrange key range
    127.0.0.1:6379 > ######################## 
    127.0.0.1:6379> zrange fan 0 -1
    1) "jin"
    2) "zhao"
    3) "zhang"
    4) "wen"
    5) "li"
    127.0.0.1:6379 > zrevrange key range
    127.0.0.1:6379 > ######################## 
    127.0.0.1:6379> zrevrange fan 0 -1
    1) "li"
    2) "wen"
    3) "zhang"
    4) "zhao"
    5) "jin"
    127.0.0.1:6379 > # zcard key. Equivalent to count ()
    127.0.0.1:6379> zcard fan
    (integer) 5
    127.0.0.1:6379 > ා get the score of the specified value
    127.0.0.1:6379> ## zscore key value
    127.0.0.1:6379> zscore fan wen
    "100"
    127.0.0.1:6379 > ා get the ranking of the specified value
    127.0.0.1:6379> ## zrank key value
    127.0.0.1:6379> zrank fan wen
    (integer) 3
    127.0.0.1:6379 > ා get the data in the specified range
    127.0.0.1:6379 > zrangebycore key range
    127.0.0.1:6379> zrangebyscore fan 99 100
    1) "zhang"
    2) "wen"
    127.0.0.1:6379 > ා get the data in the specified range
    127.0.0.1:6379 > # zrangebyscore key - inf inf [only value]
    127.0.0.1:6379 > # zrangebycore key - inf inf WithCores [value and score returned at the same time]
    127.0.0.1:6379 > # - inf / inf stands for infinity, infinity
    127.0.0.1:6379> zrangebyscore fan -inf inf
    1) "jin"
    2) "zhao"
    3) "zhang"
    4) "wen"
    5) "li"
    127.0.0.1:6379> zrangebyscore fan -inf inf withscores
     1) "jin"
     2) "97"
     3) "zhao"
     4) "98"
     5) "zhang"
     6) "99"
     7) "wen"
     8) "100"
     9) "li"
    10) "101"
    127.0.0.1:6379> zrange fan 0 -1
    1) "jin"
    2) "zhao"
    3) "zhang"
    4) "wen"
    5) "li"
    127.0.0.1:6379 > delete value
    127.0.0.1:6379> ## zrem key value
    127.0.0.1:6379> zrem fan wen
    (integer) 1
    127.0.0.1:6379> zrange fan 0 -1
    1) "jin"
    2) "zhao"
    3) "zhang"
    4) "li"
    127.0.0.1:6379>

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]