Redis implementation information Read unread status hint _redis

Source: Internet
Author: User
Tags add time redis

This article for everyone to share the Redis implementation information has read unread status prompts the key code, hoping to give you some inspiration, the specific contents are as follows

Premise:

If there are now 2 modules to be prompted message: As long as the user has not seen the last time after the information will prompt the user with new information

Ideas are as follows:

Use the hash to store the time that the user last viewed, using SortedSet to store the time each information generated for each module

Code on:

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=
  //Here Gets the user's operation time set
  map<string, string> Map = Jedis.hgetall ("u-key-" +uid);
  if (map = = null) {
    map = new hashmap<> ();
  }
  for (string value:cagoryarray) {
    //Get last operation time under a classification
    string s = map.get (value);
    if (Stringutils.isblank (s)) {
      //If not present, set to have new information
      datamap.put (value, "1");
    } else {
      / Calculates the new amount of information from the last operation time to the present number
      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 without new information
        datamap.put (value, "0");
      } else {
        Datamap . put (Value, "1");}}}
 
Finally {
  if (jedis!=null) {
    //return Connection
  }
}


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

Jedis jedis=null;
The C1 module has a new information
String cid= "C1";
 
try {
  //connection pool get connection jedis=
 
  //Add to SortedSet result weight is time millisecond
  long currenttimemillis = System.currenttimemillis ();
  Jedis.zadd ("c-key-" +cid, Currenttimemillis, string.valueof (Currenttimemillis));
 
Finally {
  if (jedis!=null) {
    //return Connection
  }
}

When a user clicks on a module, updates the user's last time to view the module:

Jedis jedis=null;
The C1 module has a new information
String cid= "C1";
User ID
String uid= "1";
 
try {
  //connection pool get connection jedis=
 
  //Add to SortedSet result weight for time millisecond
  jedis.hset ("u-key-" +uid, CID, string.valueof ( System.currenttimemillis ());
 
} Finally {
  if (jedis!=null) {
    //return Connection
  }
}

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.