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

  1. The key is the user ID
  2. The commodity ID is Field
  3. The quantity of goods is value

Shopping cart operation:

  1. Add item Hset Cart :1001 1088 1
  2. Increased quantity hincrby Cart :1001 1088 1
  3. Total goods hlen CART :1001
  4. Delete item hdel Cart :1001 1088
  5. Get all displayed goods HGEtall Cart :1001

The advantages of a Hash

  1. The same kind of data is integrated and stored for convenient data management
  2. It consumes less memory and CPU than String operations
  3. More space efficient than String storage

The disadvantage of the Hash

  1. Expiration is not on field, only on Key
  2. 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

  1. 36 Send a push message. The message ID is 1001
lpush msg:{zhangsan id} 1001 
Copy the code
  1. Autohome push message, message ID 1002
lpush msg:{zhangsan id} 1002
Copy the code
  1. 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

  1. The first online user
  2. 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

  1. Click participate to join the collection
sadd key {userId}
Copy the code
  1. View all users participating in the draw
smembers key
Copy the code
  1. 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

  1. give a like
Sadd like:{message id} {user id}Copy the code
  1. Cancel the thumb up
Srem like:{message id} {user id}Copy the code
  1. Check if the user likes it
Sismember like:{message id} {user id}Copy the code
  1. Gets a list of likes
Smembers like:{message id}Copy the code
  1. 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

  1. People that Zhang SAN cares about
zhangsan -> {lisi , wangmazi}
Copy the code
  1. The person that Li Si cares about
lisi -> {zhaoliu, wangmazi}
Copy the code
  1. The people that Pockmarked Wang cared about
wangmazi -> {lisi, zhangsan, wangwu}
Copy the code
  1. Someone that Tom and Tom share an interest in
sinter zhangsan lisi -> {wangmazi}
Copy the code
  1. The people in the Chang SAN Can group also paid attention to Li Si
sismember zhangsan lisi
sisimember wangmazi lisi
Copy the code
  1. 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

  1. Click on the news
Zincrby Hotnews :20210120 1 China space 2021 off to a good startCopy the code
  1. Top 10 on display
zrevrange hotnews:20210120 0 9 withscores
Copy the code
  1. 7 days search list calculation
zunionstore hotnews:20210114 - hotnews:20210120  10
Copy the code
  1. Show the top ten seven days
zrevrange hotnews:20210114 - hotnews:20210120  0 9 withscores
Copy the code