Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the application scenarios of Redis?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about the application scenarios of Redis, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Caching

String Typ

For example: hot data cache (such as reports, star derailment), object cache, full-page cache, access data that can improve hot data.

2. Data sharing and distribution

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

For example: distributed Session

Org.springframework.session spring-session-data-redis 3, distributed Lock

The setnx method of type String, which can be added successfully only if it does not exist, 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

In the scene of sub-database and sub-table, take one section at a time.

5. Counter

Int type, incr method

For example: the number of articles read, the number of likes on Weibo, allow a certain delay, first write Redis and then regularly synchronize to the database

6. Current restriction

Int type, incr method

Use the visitor's ip and other information as key. Increase the count one time at a time, and return false if the number of visits exceeds

7. Bit statistics

Bitcount of String type (introduction to bitmap data structure of 1.6.6)

Characters are stored in 8-bit binary

Set K1 asetbit K1 6 1setbit K1 7 0get K1 / * 67 represents a binary bit modification a corresponding ASCII code is 97, converted to binary data is 01100001b corresponding to ASCII code is 98, converted to binary data is 01100010 because bit is very space-saving (1 MB=8388608 bit), can be used to do large data statistics. , /

For example: online user statistics, retained user statistics

Setbit onlineusers 01 setbit onlineusers 11 setbit onlineusers 20

Support bitwise and, bitwise or, etc.

BITOPANDdestkeykey [key...], logically join one or more key and save the result to destkey. BITOPORdestkeykey [key...], logically OR one or more key, and save the result to destkey. BITOPXORdestkeykey [key...], logically XOR one or more key, and save the result to destkey. BITOPNOTdestkeykey, logically negate 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 the hash that String can do can be done.

Key: user id;field: commodity id;value: commodity quantity.

+ 1:hincr. -1:hdecr. Delete: hdel. All: hgetall. Number of goods: hlen.

9. User message timeline timeline

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

10. Message queue

List provides two blocking pop-up operations: blpop/brpop, which can set the timeout

Blpop:blpop key1 timeout removes and fetches the first element of the list, and if there are no elements in the list, it blocks the list until the wait times out or until a popup element is found.

Brpop:brpop key1 timeout removes and fetches the last element of the list, and if there are no elements in the list, it blocks the list until the wait times out or until a popup element is found.

The operation above. It is actually the blocking queue of java. The more you learn. The lower the cost of learning

Queue: first-in division: rpush blpop, left head and right tail, right into queue, left out of queue

Stack: first in, last out: rpush brpop

11. Lucky draw

Bring a randomly obtained value

Spop myset12, like, sign in, sign in

If the Weibo ID above is t1001, the user ID is u3001

Use like:t1001 to maintain all likes of t1001 Weibo.

Liked this Weibo post: sadd like:t1001 U3001

Cancel likes: srem like:t1001 u3001

Whether to like it or not: sismember like:t1001 u3001

All users who like it: smembers like:t1001

Number of likes: scard 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.

The picture of sadd tags:i5001 is clear and delicate.

Sadd tags:i5001 true color clear display screen

Sadd tags:i5001 process is the ultimate.

14. Product screening / / get difference sdiff set1 set2// get intersection (intersection) sinter set1 set2// get union sunion set1 set2

Suppose: iPhone11 is listed.

Sadd brand:apple iPhone11sadd brand:ios iPhone11sad screensize:6.0-6.24 iPhone11sad screentype:lcd iPhone11

Selected 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:lcd15, user focus, recommendation model

Follow follows fans fans

Follow each other:

Sadd 1:follow 2

Sadd 2:fans 1

Sadd 1:fans 2

Sadd 2:follow 1

The people I follow also follow him (take intersection):

Sinter 1:follow 2:fans

People you might know:

People that user 1 may know (subtraction): sdiff 2:follow 1:follow

Person that user 2 may know: sdiff 1:follow 2:follow

16. Ranking

Add 1 to news clicks with an id of 6001:

Zincrby hotNews:20190926 1 n6001

Get the 15 most clicked on today:

Zrevrange hotNews:20190926 0 15 withscores

Redis is well used, and you can't miss a raise.

After reading the above, do you have any further understanding of the application scenarios of Redis? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report