preface

Redis is an open source, net-based, memory-based, persistent, logging, key-value database written in ANSI C, and provides apis in multiple languages.

In this article, the main application scenarios of Redis are introduced.

1, caching,

Type String

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

2. Data sharing and distribution

String, 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>
Copy the code

3. Distributed locking

The setnx method is a String that can only be added if it does not exist. It 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);
}
Copy the code

4. Global ID

The int type, incrby, takes advantage of atomicity

incrby userid 1000

Sub-database sub-table scene, a one-time take a paragraph

5. Counter

Int, incr method

For example, the number of articles read, the number of microblog likes, allowing a certain delay, are first written to Redis and then periodically synchronized to the database

6, current limiting

Int, incr method

Use the visitor’s IP and other information as the key, and count each time the visitor visits

7. Bit statistics

String bitCount (1.6.6 bitmap)

Characters are stored in 8-bit binary

Set k1 a setbit k1 6 1 setbit k1 7 0 get k1 /* 6 The binary data is 01100010 because the bit is very space-saving (1 MB=8388608 bit) and can be used for statistics of large data volumes. * /Copy the code

For example, online user statistics and retained user statistics

setbit onlineusers 01 
setbit onlineusers 11 
setbit onlineusers 20
Copy the code

Supports bitwise and, bitwise or, and so on

BITOPANDdestkeykey[key...] , to find the logic of one or more keys, and save the result to the destkey. BITOPORdestkeykey[key...] , find the logical or for one or more keys, and save the result to the destkey. BITOPXORdestkeykey[key...] , find the logical xor for one or more keys, and save the result to the destkey. BITOPNOTdestkeykey: Finds a logical non for a given key and saves the result to the destKey.Copy the code

Count the number of users who have been online for 7 days

BITOP "AND" "7_days_both_online_users" "day_1_online_users" "day_2_online_users" ...  "day_7_online_users"
Copy the code

8. Shopping Cart

String or hash. All the hashes that String can do

  • Key: user ID; Field: Product ID; Value: quantity of goods.
  • + 1: hincr. 1: hdecr. Delete: hdel. Select all: hGEtall. Number of items: Hlen.

9. User message timeline

List, two-way linked list, just as timeline. Insert the orderly

Message queue

The List provides two blocking pop-up operations: BLpop /brpop, and you can set a timeout

  • Blpop: blpop key1 timeout Removes and retrieves the first element of the list. If the list has no elements, it blocks the list until a timeout or an eject element is found.
  • Brpop: brpop key1 timeout Removes and retrieves the last element of the list. If the list has no elements, it blocks the list until a timeout or a eject element is found.

The above operation. This is essentially a Java blocking queue. The more you learn. The lower the cost of learning

  • Queue: first in first out: Rpush BLPOP, left head and right tail, right in queue, left out queue
  • Stack: first in, last out: Rpush brPOP

11, draw

It comes with a random value

spop myset
Copy the code

12. Like, sign in, and punch in

Suppose the above microblog ID is t1001 and the user ID is U3001

Use like:t1001 to maintain all “like” users of the tweet t1001

  • Sadd Like: T1001 U3001
  • Unlike: SREM like: T1001 U3001
  • Sismember Like: T1001 U3001
  • Smembers like:t1001
  • Scard Like: T1001

Is not much simpler than the database.

13. Commodity labels

As usual, use tags:i5001 to maintain all tags on your product.

  • Sadd tags:i5001 Clear and delicate
  • Sadd tags:i5001 True color clear display
  • Sadd Tags: I5001 The process is extreme

14. Commodity screening

Sdiff set1 set2; // Capture the intersection sinter set1 set2; // Capture the sunion set1 set2Copy the code

Suppose: the iPhone11 comes out

Sadd Brand: Apple iPhone11 Sadd Brand :ios iPhone11 Sad Screensize :6.0-6.24 iPhone11 Sad Screentype: LCD iPhone11Copy the code

Select products, apple, ios, screen between 6.0-6.24, screen material is LCD screen

Sinter Brand: Apple Brand :ios Screensize :6.0-6.24 Screentype: LCDCopy the code

15. User attention and recommendation model

Follow fans

Mutual attention:

  • sadd 1:follow 2
  • sadd 2:fans 1
  • sadd 1:fans 2
  • sadd 2:follow 1

The people I follow follow him:

  • sinter 1:follow 2:fans

People you may know:

  • People (difference set) that user 1 May know: sdiff 2: Follow 1: Follow
  • Sdiff 1: Follow 2: Follow

16. Leaderboard

Zincrby hotNews:20190926 1 N6001

Get today’s top 15 hits: zrevrange hotNews:20190926 0 15 withscores