16 common usage scenarios of redis


16 common usage scenarios of redis

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


3. Distributed lock

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) {

4. Global ID

Int type, incrby, using atomicity

incrby userid 1000

In the scene 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 bit 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 for 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...] , Perform logical Union on one or more keys and save the results to destkey.       
BITOPORdestkeykey[key...] , Find a logical or for one or more keys and save the result 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

  • 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 is no element in the list, the list will be blocked until the waiting timeout or pop-up element is found.
  • Brpop: brpop key1 timeout removes and gets the last element of the list. If there is no element in the list, the list will be blocked until the waiting timeout or pop-up element is found.

The above operation. In fact, it is the blocking queue of Java. The more 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

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

The old rule is to use Tags: i5001 to maintain all labels of goods.

  • Sadd Tags: i5001 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

If: iphone11 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:

zincrby hotNews:20190926 1 n6001

Get the top 15 hits today:

zrevrange hotNews:20190926 0 15 withscores

This is the end of this article about 16 common usage scenarios of redis. For more information about common scenarios of redis, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!