Redis implements the read unread status prompt
- 2020-05-12 06:25:44
- OfStack
This article shares the key code of Redis implementation information that has been read and unread status. I hope it can give you some inspiration. The details are as follows
Premise:
Let's say there are two modules that need a prompt message: if there is a message that the user hasn't seen since the last point in time, it will prompt the user for a new message
The idea is as follows:
hash is used to store the last time the user saw it, and sortedset is used to store the time that each piece of information was generated for each module
The code:
Map<String, String> dataMap = new HashMap<>();
Jedis jedis=null;
String uid="1";// The user id
// Classification of the array
String []cagoryArray={"c1","c2"};
try {
// The connection pool gets the connection jedis=
// Get the user's operation time set here
Map<String, String> map = jedis.hgetAll("u-key-"+uid);
if (map == null) {
map = new HashMap<>();
}
for (String value : cagoryArray) {
// Gets the last operation time under a category
String s = map.get(value);
if (StringUtils.isBlank(s)) {
// If it does not exist, set it to have new information
dataMap.put(value, "1");
} else {
// Calculate the amount of new information from the last operation time to now
Long zcount = jedis.zcount("c-key-"+value, Double.parseDouble(s), System.currentTimeMillis());
if (zcount == null || zcount <= 0) {
// Does not exist or is less than or equal to 0 There is no new information
dataMap.put(value, "0");
} else {
dataMap.put(value, "1");
}
}
}
}finally {
if(jedis!=null){
// Return the connection
}
}
When new information is generated, add time to the relevant module:
Jedis jedis=null;
//c1 The module has new information
String cid="c1";
try {
// The connection pool gets the connection jedis=
// Added to the sortedset The results of The weight is in milliseconds
long currentTimeMillis = System.currentTimeMillis();
jedis.zadd("c-key-"+cid, currentTimeMillis, String.valueOf(currentTimeMillis));
}finally {
if(jedis!=null){
// Return the connection
}
}
When the user clicks on a module, update the last time the user viewed the module:
Jedis jedis=null;
//c1 The module has new information
String cid="c1";
// The user id
String uid="1";
try {
// The connection pool gets the connection jedis=
// Added to the sortedset The results of The weight is in milliseconds
jedis.hset("u-key-"+uid, cid, String.valueOf(System.currentTimeMillis()));
}finally {
if(jedis!=null){
// Return the connection
}
}
The above is the entire content of this article, I hope to help you with your study.