Redis is expected

  • Introduce the use of five data types, Redis transactions, publish and subscribe, persistence, master and slave, Sentinel, high availability cluster, etc… Specific how many articles to write more detailed depends on the mood

Redis data typeOperation Command Reference

  • Here I am using the docker installation… Save your effort. Just a few lines of command. The downside is that people unfamiliar with Redis are still unfamiliar with its necessary configuration

Once installed

Redis docker exec it redis-test (container name) /bin/bash redis-cli -p 6379 For example help @stirng so I'm not going to go into commands, I'm going to go into what I want to search for on redis, rightCopy the code

String Application Scenario

  • Single value cache

set key value get key

  • Object caching
Set user:1 value(JSON format data) can also be used for batch storage (used to frequently modify one or two fields of a user, stored by KEY). 2 User :1:sex Male mget user:1:name User :1:sexCopy the code
  • Distributed locks (Entry level)
Setnx product:101 true setnx product:101 true 0 failed to obtain the lock.. Service del product:10001 // Release the lock after executing the service set product:10001 true ex 10 Nx // Prevent the deadlock caused by the termination of the programCopy the code
  • counter
Incr article:readcount:{article id} get article:readcount:{article id}Copy the code
  • Web cluster Session sharing
I remember that there are many implementations of Spring session (e.g. Mysql, ZooKeeper,mongodb). Redis is just a spring Session + Redis implementation of session sharingCopy the code
  • Distributed system global serial number
Incrby OrderID 100 // Batch generate sequence number to improve performance Purpose: to reduce interaction with Redis, ++ in memory, and finally interact with Redis. For example, a large amount of data needs to be generated at the same time, and the database can not use the self-increasing ID in the sub-tableCopy the code

Common Hash applications

Hash structure: Key value -> key value in vlue

  • Object caching
User example (batch) hmset user {userId}:name Zhang3 {userId}:sex male hmset user 1:name Zhang3 1:sex male hmget user 1:name 1:balance If too many users.... There will be bigkey issues (after all, it's a single-threaded thing, and there will be delays, which will cause blocking). This is where segmented storage is requiredCopy the code
  • Shopping cart (common)
Hset cart:{user ID} {merchandise ID} {quantity} hset cart:1 111 1 hset cart:1 222 1 2. Increased item quantity hincrby Cart :1 111 1 3. Hlen Cart :1 4. Delete cart:1 111 5. Get all items in the cart hGEtall Cart :1Copy the code
  • Compare to string:
Advantages: 1. Similar data classification and integrated storage, convenient data management (one key to manage multiple users) 2. Smaller memory and CPU than string operations (2 and 3 due to underlying data structures) 3. Disadvantages: 1. Outdated functions cannot be used on fields, but only on keys. 2. Redis cluster architecture is not suitable for large-scale use (cannot be evenly distributed (can also be solved))Copy the code

list

Because we don’t use it very much, we should mention that we have a value for each key

  • Common operations
lpush key value[...] From the left put Lpush zhang SAN Li Si Lpush Wang Wu Zhao Liu Rpush... Remove lPOP key from the right and return the first element of the key table (remove the element from the left and remove it) // Return a list of key elements within the specified range, with the offset start and stop specified by blPOP key [key..] Brpop key [key...] brPOP key [key...] brPOP key [key...] Timeout Pops an element from the end of the list. If there is no element in the list, block and wait timeout seconds. If timeout = 0, block and wait foreverCopy the code
  • Common data structures
Stack = Lpush + LPOP (first int last out) queue = Lpush + RPOP blocking MQ = Lpush + BRPOPCopy the code
  • Wechat moments
Lpush MSG :{zhang SAN's Id} {Wang Wu's friend circle} 3 lpush MSG :{Zhang SAN's Id} {Wang Wu's friend circle} 3 Lrange MSG :{lrange MSG :{lrange MSG :{lrange MSG:} 0Copy the code

set

A key corresponds to a set

  • Common operations
sadd key member[member...] Srem key member [member...] Smember key scard key sismember key member SrandMember key [count] Spop key [count] Select count elements and delete them from keyCopy the code
  • Arithmetic operations
sinter key [key...] Intersection operation (data intersected by multiple sets). Sunion key[key] sunionStore Destination key[key...] Sdiff key [key.,..] Sdiffstore destination key [key...] sdiffstore destination key Store the difference set result in the new collection destinationCopy the code
  • WeChat thumb up
Sadd like:{message id} {user id} 2. Srem like:{message id} {user ID} 3. Check whether the user has clicked sismember like:{message id} Scard like: {message id}Copy the code
  • Set operation to achieve wechat concern model
1. ZhangsanSet ->{wang wu, wang zhaojun, anqila} 2. LiseSet ->{Zhang SAN, Wang Zhaojun, Anqira, Dian Wei, Mo Zi} 3. Zhang SAN, Li Si, Dian Wei, Zhao Yun} 4. Sinter zhangsanSet lisiSet ={Wang Zhaojun} 5. WangzhaojunSet Wangmember Sismember anqilaSet 6. Sdiff lisiSet zhangsanSet = {Zhang SAN, Dian Wei, Mo Zi}Copy the code
  • Achieve e-commerce screening
sadd brand:huawei huaweiMate40 p40
sadd os:ios iphone100 iphone101
sadd ram:16G iphone100 huaweiMate40
sinter brand:huawei ram:16G  = {huaweiMate40}
Copy the code

zset

  • Zset Common operations
zadd key score member [[score member]...] Zrem key member [member...] Zincrby key increment Member Zincrby key increment member Zincrby key increment member Zincrby key increment member Zcard key Returns the number of elements in the ordered set key. Zrange key start stop [withscores] Obtains elements in the ordered set key from start to stop. Zrevrange key start stop [withscores] retrieves the ordered set key from the table below start to the index below stopCopy the code
  • Zset set operation
zunionstore destkey numkeys key[key ...] Zinterstore destKey numkeys key[key....] Intersection operationCopy the code
  • Zset set operation to achieve the leaderboard
Zincrby hotNews:20210619 Zrevrange hotNews:20200220 0 9 withscores 3. 7 days search list calculation zUnionStore hotNews:20200602-20200608 7 hotNews:20200602 hotNews:20200603... Zrevrange hotNews:20200601-20200610 0 9 withscoresCopy the code