1. Cache
String type
For example: hot data caching (such as reports, celebrities cheating), object caching, full page caching, and access data that can improve hot data.
2. Data sharing and distributed
String type, because Redis is a distributed independent service, which 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
String type setnx method, can be added successfully only if it does not exist, return 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, use atomicity
incrby userid 1000
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 Weibo likes, a certain delay is allowed, first write to Redis and then synchronize to the database regularly
6. Current limit
int type, incr method
With the visitor's ip and other information as the key, the count will be incremented once for each visit, and false will be returned if the number of visits is exceeded.
7. Bit statistics
String type bitcount (introduction of 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
/* 6 7 代表的a的二进制位的修改
a 对应的ASCII码是97,转换为二进制数据是01100001
b 对应的ASCII码是98,转换为二进制数据是01100010
因为bit非常节省空间(1 MB=8388608 bit),可以用来做大数据量的统计。
*/
For example: online user statistics, retention 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 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:用户id;field:商品id;value:商品数量。
+1:hincr。-1:hdecr。删除:hdel。全选:hgetall。商品数:hlen。
9. User message timeline timeline
list, doubly linked list, just as timeline directly. Insert order
10. Message queue
List provides two blocking pop operations: blpop/brpop, you can set the timeout
- blpop: blpop key1 timeout Remove and get the first element of the list. If there are no elements in the list, the list will be blocked until the waiting timeout or an element that can be popped is found.
- brpop: brpop key1 timeout Remove and get the last element of the list. If there are no elements in the list, the list will be blocked until the waiting timeout or an element that can be popped is found.
The operation above. In fact, it is the blocking queue of java. The more things you learn. The lower the cost of learning
- Queue: First-in, first-divide: rpush blpop, left head and right tail, enter the queue on the right, and leave the queue on the left
- Stack: first in, last out: rpush brpop
11. Lottery
Comes with a random value
spop myset
12. Like, sign in, punch in
If the Weibo ID above is t1001 and the user ID is u3001
Use like:t1001 to maintain all like users of this Weibo t1001
- Liked this Weibo: sadd like:t1001 u3001
- Unlike: srem like:t1001 u3001
- Like or not: sismember like:t1001 u3001
- All users who liked: smembers like:t1001
- Number of likes: scar like:t1001
Is it much simpler than the database. In addition, follow the Java PhoenixMiles public account, reply to the "back-end interview", and send you a collection of interview questions!
13. Product label
The old rule is to use tags:i5001 to maintain all the tags of the product.
- sadd tags:i5001 clear and delicate picture
- sadd tags:i5001 true color clear display
- sadd tags:i5001 The extreme process
14. Product screening
// 获取差集
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
Selected products, Apple, ios, screens between 6.0-6.24, the screen material is LCD screen
sinter brand:apple brand:ios screensize:6.0-6.24 screentype:lcd
15. User attention and recommendation model
follow Follow fans
Pay attention to 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 the intersection):
sinter 1:follow 2:fans
people you may know:
用户1可能认识的人(差集):sdiff 2:follow 1:follow
用户2可能认识的人:sdiff 1:follow 2:follow
16. Ranking
The number of news hits with id 6001 is increased by 1: zincrby hotNews:20190926 1 n6001
Get the 15 most clicked today: zrevrange hotNews:20190926 0 15 withscores
blog.csdn.net/qq_39938758/article/details/105577370
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。