Take you to thoroughly understand the 16 application scenarios of redis!

Time:2021-9-23

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 
/*Modification of binary bits of a represented by 6 and 7

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

Bitonanddestkeykey [key...], logically combine one or more keys and save the results to destkey.       
Bitopordestkeykey [key...], find a logical or for one or more keys, and save the results to destkey. 
Bitopxordestkeykey [key...], find logical XOR for one or more keys and save the results to destkey. 
Bitopnotdestkeykey: find the logical negation of the given key 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

Take you to thoroughly understand the 16 application scenarios of redis!

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

Take you to thoroughly understand the 16 application scenarios of redis!

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. In addition, pay attention to the Java bosom friend public account, reply to the “back-end interview” and send you a treasure book of interview questions!

13. Commodity label

Take you to thoroughly understand the 16 application scenarios of redis!

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

Take you to thoroughly understand the 16 application scenarios of redis!

What if: the iPhone 11 is on the market

sadd brand:apple iPhone11

sadd brand:ios iPhone11

sad screensize:6.0-6.24 iPhone11

sad screentype:lcd iPhone 11

The selected products are Apple’s, IOS’s, and the screen is between 6.0 and 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

Take you to thoroughly understand the 16 application scenarios of redis!

blog.csdn.net/qq_39938758/article/details/105577370

Take you to thoroughly understand the 16 application scenarios of redis!

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]