Where can Redis be applied? This article will share with you 16 common usage scenarios of Redis in one go. I hope it will be helpful to everyone!
[Related recommendations: Redis video tutorial]
String type
For example: hot data cache (such as reports, celebrity cheating), object cache, full page cache, access data that can improve hot data.
String type, because Redis is a distributed independent service and can be shared between multiple applications
For example: Distributed Session
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
String type setnx method can only be added successfully if 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); }
int type, incrby, using atomicity
incrby userid 1000
In the scenario of sub-database and sub-table, get a section at one time
int type, incr method
For example: the number of articles read, the number of Weibo likes, allow a certain delay, first write to Redis and then synchronize to the database regularly
int type, incr method
uses the visitor's IP and other information as the key. Each visit increases the count. If the number exceeds the number, false is returned
Bitcount of String type (bitmap data structure introduction 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 代表的a的二进制位的修改 a 对应的ASCII码是97,转换为二进制数据是01100001 b 对应的ASCII码是98,转换为二进制数据是01100010 因为bit非常节省空间(1 MB=8388608 bit),可以用来做大数据量的统计。 */
For example: online user statistics , retain user statistics
setbit onlineusers 01 setbit onlineusers 11 setbit onlineusers 20
Support bitwise AND, bitwise OR, etc. operations
BITOPANDdestkeykey[key...] ,对一个或多个 key 求逻辑并,并将结果保存到 destkey 。 BITOPORdestkeykey[key...] ,对一个或多个 key 求逻辑或,并将结果保存到 destkey 。 BITOPXORdestkeykey[key...] ,对一个或多个 key 求逻辑异或,并将结果保存到 destkey 。 BITOPNOTdestkeykey ,对给定 key 求逻辑非,并将结果保存到 destkey 。
Calculate 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"
String or hash. All hashes that can be done with String can be done
list, a doubly linked list, can be used directly as timeline. Insertion order
List provides two blocking pop-up operations: blpop/brpop, and the timeout can be set
The above operation. In fact, it is Java's blocking queue. The more things you learn. The lower the learning cost
Comes with a random value
spop myset
Suppose the above Weibo ID is t1001 and the user ID is u3001
Use like:t1001 to maintain t1001. All users who liked this Weibo
Isn’t it much simpler than the database?
The old rule is to use tags:i5001 to maintain all tags of the product.
// 获取差集 sdiff set1 set2 // 获取交集(intersection ) sinter set1 set2 // 获取并集 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
Select products, Apple, ios, screen between 6.0-6.24 Sometimes, the screen material is LCD screen
sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd
follow Follow fans Fans
Follow each other:
I follow People also followed him (take intersection):
People you may know:
id 为6001 的新闻点击数加1:
zincrby hotNews:20190926 1 n6001
获取今天点击最多的15条:
zrevrange hotNews:20190926 0 15 withscores
Redis 用的好,加薪少不了
原文地址:https://juejin.cn/post/6994229128534687781
作者:码猿技术专栏
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of Where can Redis be used? 16 common usage scenarios shared. For more information, please follow other related articles on the PHP Chinese website!