An overview of the
There are five common data types: String, list, set, Hash, and zset
1. string
String Common operations
set key value
get key
Copy the code
Object caching
Set user:1 value(Json formatted data) 2. mset user:1:name huawei user1:balance 2000 mget user:1:name user:1balanceCopy the code
A distributed lock
Setnx product:10001 true // If 1 is returned, the lock is successfully obtained. Setnx product:10001 true // If 0 is returned, the lock is successfully obtained..... Run the service logic del product:10001 // Release the lock after running the service set product:10001 true ex 10 Nx // Prevent the final deadlock caused by unexpected interruptionCopy the code
counter
Incr article:forward:{article id} get article:forward:{article id}Copy the code
Web cluster Session sharing
Spring Session + Redis implements session sharing
Distributed global serial number
Incrby orderId 1000 // Redis batch generation sequence number to improve performance batch generation ID, there is a step size, through the concept of space for time, to reduce the interaction with redis, improve the new with that
Common string operations
Mset key value [key value] // Batch store string key value setnx key Value // Store a nonexistent string key value get key // Get a string key-value pair mget key [key.. Del key [key...] // Delete key expire key seconds // set key expiration time (s)Copy the code
Atoms to add and subtract
Incr key add decr key add decr key add incrby key increment Add increment DECrby key add increment DECrby key Decrement // Subtracts increment from the values stored in the keyCopy the code
2. hash
Common Hash Operations
Hset key field value hsetnx key field Value hsetnx key field Value hmset key field [field value...] Hget key field [field...] Hdel key field [field...] Hgetall key hgetall key hincrby key field increment // Mysql > alter table key increment Increment A large number of data stored in the same key is BigKeyCopy the code
Hash Application Scenarios
Object caching
hmset user {userid}:name zhangsan {userid:}:balance 1888
hmset user 1:name zhangsan 1:balance 1888
hmget user 1:name 1:balance
Copy the code
E-commerce shopping cart
- The key is the user ID
- The commodity ID is Field
- The quantity of goods is value
Shopping cart operation:
- Add item Hset Cart :1001 1088 1
- Increased quantity hincrby Cart :1001 1088 1
- Total goods hlen CART :1001
- Delete item hdel Cart :1001 1088
- Get all displayed goods HGEtall Cart :1001
The advantages of a Hash
- The same kind of data is integrated and stored for convenient data management
- It consumes less memory and CPU than String operations
- More space efficient than String storage
The disadvantage of the Hash
- Expiration is not on field, only on Key
- The Redis cluster architecture is not well suited for large-scale use
3. list
List Common Operations
lpush key value [value...] Rpush key value [value...] rpush key value [value...] Rpop Key // Remove and return the first element of the key list. Lrang key srat stop // Remove and return the last element of the key list Blpop key [key,,,] timeout // If there is no element in the list, block wait timeout seconds. If timeout = 0, block waiting for brPOP key [key... Timeout // Pops an element from the end of the key list. If there is no element in the list, block and wait timeout seconds. If timeout = 0, block and wait foreverCopy the code
Usage scenarios
Lpush + LPOP = FIFO Queue lpush + RPOP Blocking MQ = Lpush + BRPOP
Weibo and wechat message flows
Zhang SAN paid attention to 36 krypton, between cars and other public numbers
- 36 Send a push message. The message ID is 1001
lpush msg:{zhangsan id} 1001
Copy the code
- Autohome push message, message ID 1002
lpush msg:{zhangsan id} 1002
Copy the code
- Get the latest news
lrange msg:{zhangsan id} 0 4
Copy the code
Problem: If Big V follows tens of millions of users, then we can send messages in batches
- The first online user
- Then the background batch to other users
4. set
Set common operations
sadd key member [member ...] // create srem key member [member...] Scard key // Simember key member // Check whether the member element exists in the set Srangmembre key [count] // Select count from set. Spop key [count] spop key [count] spop key [countCopy the code
Set operation
sinter key [key..] // Add destination key [key...] // store the intersection result ina new set destination sunion key [key...] Sunionstore destination key [key...] Sdiff key [key...] Sdiffstore destination key [key...] // Store the difference set result ina new collection destinationCopy the code
Usage scenarios
1) Wechat lottery function
- Click participate to join the collection
sadd key {userId}
Copy the code
- View all users participating in the draw
smembers key
Copy the code
- Extract the middle of count
Srandmember key [count] / spop key [count] SrandMember key [count] / spop key [countCopy the code
2) Like, favorites and tags on wechat and Sina Weibo
- give a like
Sadd like:{message id} {user id}Copy the code
- Cancel the thumb up
Srem like:{message id} {user id}Copy the code
- Check if the user likes it
Sismember like:{message id} {user id}Copy the code
- Gets a list of likes
Smembers like:{message id}Copy the code
- Get the number of likes
Scard like:{message id}Copy the code
Set operations
Sinter set1 set2 set3 -> {c} sumion set1 set2 set3 -> {a, b, c, d,e} The first set minus the union of the following setsCopy the code
Set operation to realize the micro blog concern model
- People that Zhang SAN cares about
zhangsan -> {lisi , wangmazi}
Copy the code
- The person that Li Si cares about
lisi -> {zhaoliu, wangmazi}
Copy the code
- The people that Pockmarked Wang cared about
wangmazi -> {lisi, zhangsan, wangwu}
Copy the code
- Someone that Tom and Tom share an interest in
sinter zhangsan lisi -> {wangmazi}
Copy the code
- The people in the Chang SAN Can group also paid attention to Li Si
sismember zhangsan lisi
sisimember wangmazi lisi
Copy the code
- Someone I might know
sdiff lisi zhangsan -> {zhaoliu}
Copy the code
Set operation to achieve e-commerce commodity screening
sadd brand:huawei P40
sadd brand:xiaomi mi-11
sadd brand:iPhone iphone11
sadd os:android P40 mi-11
sadd cpu:brand:intel P40 mi-11
sadd ram:8G mi-11 iphone11
sinter os:andorid cpu:brand:intel ram:8G -> {P40, mi-11}
Copy the code
5. zset
Zset Common operations
zadd key score member [[score member] ... ] Zrem key member [member...] Zrange key start stop [withscores] zrange key start stop [withscores Zrevrages key start stop [withscores] // Get ordered set key from start subscript to stop subscript zset set operation zunionStore destkey numkeys key [key ...] Zunionstore destKey numkeys key [key...] // Union calculationCopy the code
Hot search recommendation
- Click on the news
Zincrby Hotnews :20210120 1 China space 2021 off to a good startCopy the code
- Top 10 on display
zrevrange hotnews:20210120 0 9 withscores
Copy the code
- 7 days search list calculation
zunionstore hotnews:20210114 - hotnews:20210120 10
Copy the code
- Show the top ten seven days
zrevrange hotnews:20210114 - hotnews:20210120 0 9 withscores
Copy the code