Redis implementation information read unread status prompt

Time:2020-1-7

This article shared the key code of redis implementation information read and unread status prompt, hoping to give you some inspiration. The specific content is as follows

Premise:

If there are two modules that need prompt message now: as long as there is a message that the user has not seen after the last time point, it will prompt the user to have new information

The ideas are as follows:

Use hash to store the last time the user saw it, and use sortedset to store the time when each information of each module was generated

Upper Code:

Map<String, String> dataMap = new HashMap<>();
 
Jedis jedis=null;
String uid = "1"; // user ID
//Category array
String []cagoryArray={"c1","c2"};
try {
  //Connection pool get connection jedis=
  //Get user's operation time collection here
  Map<String, String> map = jedis.hgetAll("u-key-"+uid);
  if (map == null) {
    map = new HashMap<>();
  }
  for (String value : cagoryArray) {
    //Get the last operation time under a category
    String s = map.get(value);
    if (StringUtils.isBlank(s)) {
      //If not, set 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) {
        //No new information exists or less than or equal to 0
        dataMap.put(value, "0");
      } else {
        dataMap.put(value, "1");
      }
    }
 
  }
 
}finally {
  if(jedis!=null){
    //Return connection
  }
}

When new information is generated, add time to relevant modules:

Jedis jedis=null;
//C1 module has new information
String cid="c1";
 
try {
  //Connection pool get connection jedis=
 
  //Add to sortedset result weight in milliseconds
  long currentTimeMillis = System.currentTimeMillis();
  jedis.zadd("c-key-"+cid, currentTimeMillis, String.valueOf(currentTimeMillis));
 
}finally {
  if(jedis!=null){
    //Return connection
  }
}

When the user clicks a module, update the last time the user viewed the module:

Jedis jedis=null;
//C1 module has new information
String cid="c1";
// user ID
String uid="1";
 
try {
  //Connection pool get connection jedis=
 
  //Add to sortedset result weight in milliseconds
  jedis.hset("u-key-"+uid, cid, String.valueOf(System.currentTimeMillis()));
 
}finally {
  if(jedis!=null){
    //Return connection
  }
}

The above is the whole content of this article, I hope to help you learn.