Do you know the common scenarios of redis?

Posted by KoshNaranek on Wed, 22 Sep 2021 14:12:56 +0200

1. Cache

String type

For example, hot data cache (such as reports and star cheating), object cache and full page cache can improve the access data of hot data.

2. Data sharing distributed

String type, because Redis is a distributed independent service that can be shared among multiple applications

For example: distributed Session

<dependency> 
    <groupId>org.springframework.session</groupId> 
    <artifactId>spring-session-data-redis</artifactId> 
</dependency>

3. Distributed lock

The setnx method of String type can be added successfully only when it does not exist, and returns true

public static boolean getLock(String key) {
    Long flag = jedis.setnx(key, "1");
    if (flag == 1) {
        jedis.expire(key, 10);
    }
    return flag == 1;
}

public static void releaseLock(String key) {
    jedis.del(key);
}

4. Global ID

int type, incrby, using atomicity

incrby userid 1000

For the scenario of sub database and sub table, take one paragraph at a time

5. Counter

int type, incr method

For example, the number of articles read, the number of microblog likes, and a certain delay are allowed. First write to Redis, and then synchronize to the database regularly

6. Current limiting

int type, incr method

Take the visitor's ip and other information as the key. The count is increased once for each visit. If it exceeds the number, false is returned

7. Bit statistics

bitcount of String type (Introduction to bitmap data structure in 1.6.6)

Characters are stored in 8-bit binary

set k1 a
setbit k1 6 1
setbit k1 7 0
get k1 
/* 6 7 Modification of binary bits of a
a The corresponding ASCII code is 97 and the converted binary data is 0110001
b The corresponding ASCII code is 98 and the converted binary data is 01100010

Because bit saves a lot of space (1 MB=8388608 bit), it can be used to make statistics of large amount of data.
*/

For example: online user statistics, retained user statistics

setbit onlineusers 01 
setbit onlineusers 11 
setbit onlineusers 20

Support bitwise and, bitwise or other operations

BITOPANDdestkeykey[key...] ,For one or more key Find logical Union and save the result to destkey .        
BITOPORdestkeykey[key...] ,For one or more key Find logical or and save the result to destkey .  
BITOPXORdestkeykey[key...] ,For one or more key Find logical XOR and save the result to destkey .  
BITOPNOTdestkeykey ,Given key Find logical negation and save the result to destkey . 

Calculate the users who are online for 7 days

BITOP "AND" "7_days_both_online_users" "day_1_online_users" "day_2_online_users" ...  "day_7_online_users"

8. Shopping cart

String or hash. All hashes that string can do can be done

picture

  • key: user id; field: Commodity id; value: quantity of goods.

  • +1: hincr. - 1: hdecr. Delete: hdel. Select all: hgetall. Number of products: Helen.

9. User message timeline

List, two-way linked list, directly as timeline. Insert order

10. Message queue

List provides two blocked pop-up operations: blpop/brpop, and the timeout can be set

  • Blpop: blpop key1 timeout removes and obtains the first element of the list. If there are no elements in the list, the list will be blocked until the waiting timeout or pop-up elements are found.

  • Brpop: brpop key1 timeout removes and obtains the last element of the list. If there are no elements in the list, the list will be blocked until the waiting timeout or pop-up elements are found.

The above operation. In fact, it is the blocking queue of java. The more things you learn. The lower the learning cost

  • Queue: divide first: rpush blpop, left head and right tail, enter the queue on the right and exit the queue on the left

  • Stack: first in, last out: rpush brpop

11. Draw

With a random value

spop myset

12. Like, sign in, punch in

picture

If the microblog ID above is t1001 and the user ID is u3001

Use like:t1001 to maintain all users who like t1001 microblog

  • Like this microblog: sadd like:t1001 u3001

  • Cancel likes: srem like:t1001 u3001

  • Like: sismember like:t1001 u3001

  • All users who like: smembers like:t1001

  • Number of likes: scar like: t1001

Is it much simpler than the database.

13. Commodity label

picture

As the old rule, use tags:i5001 to maintain all labels of goods.

  • sadd tags:i5001 the picture is clear and delicate

  • sadd tags:i5001 true color clear display

  • sadd tags:i5001 process to the extreme

14. Commodity screening

// Get difference set
sdiff set1 set2
// Get intersection
sinter set1 set2
// Get Union
sunion set1 set2

picture

Suppose: the iPhone 13 is on the market

sadd brand:apple iPhone13

sadd brand:ios iPhone13

sad screensize:6.0-6.24 iPhone13

sad screentype:lcd iPhone 13

Select products from apple, ios, and the screen is between 6.0-6.24. The screen material is LCD screen

sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd

15. User concern and recommendation model

follow fans

Mutual attention:

  • sadd 1:follow 2

  • sadd 2:fans 1

  • sadd 1:fans 2

  • sadd 2:follow 1

The person I pay attention to also pays attention to him (take intersection):

  • sinter 1:follow 2:fans

Possible acquaintances:

  • People that user 1 may know (difference set): sdiff 2:follow 1:follow

  • Possible acquaintances of user 2: sdiff 1:follow 2:follow

16. Ranking list

News hits with id 6001 plus 1: zinc hotnews: 20190926 1 n6001

Get the top 15 hits today: zrevrange hotnews: 20190926 0 15 WithCores

Pay attention to the official account: shrimp and share more java dry cargo

Topics: Java Redis