Use Redis to test shopping cart, counter, good / bad rating and other functions [you can understand it at a glance]

Link to the above: (if you don't know much about Spring integration Redis, please refer to)

Spring integration Redis introduction details and basic exercises_ Yang Daxian loves basketball - CSDN blog

This article only does functional testing to show the code logic, and will be released later on how to use it in the project.

Shopping cart function

The definition of shopping cart is very simple: we can implement it through hash in Redis:

  • If the user ID (or Cookie_id) of each user is used as the Key of the hash, the shopping cart of each user is a hash table, which can be used to store the product ID and the ordered quantity of products.
  • When the number of goods in the shopping cart changes, we can update the shopping cart by using the method in Redis hash;
  • If the quantity of a product ordered by the user is greater than 0, the program will add the ID of the product and the quantity of the product ordered by the user to the hash.

The effect is shown in the figure:

Corresponding test code:  

 @Test   //  Realization of shopping cart function
 public void testShopping() {
     HashOperations forHash = redisTemplate.opsForHash();
     forHash.put("user_1", "gid_1", 6);  //  Indicates that user 1 bought 6 No. 1 items
     forHash.put("user_2", "gid_3", 1);  //  Indicates that user 2 bought a No. 3 item
     //  Add or subtract the number of goods in the shopping cart
     forHash.increment("user_1", "gid_1", -1);   //  User 1 minus 1 item 1
     forHash.increment("user_2", "gid_3", 1);    //  User 2 added 1 item 3
     //  Query shopping cart
     Map user_1 = forHash.entries("user_1");
     Set set = user_1.entrySet();
     for (Object o : set) {
         System.out.print("Product information:" + o + " ");
     }
 }

The realization of shopping cart function and code logic are roughly the same, and we need to use it flexibly in actual use.

Counter function

Redis hash table is also widely used as a counter. It is often used to record the number of visits to the website every day, month and year.

  • On the first visit, we create a unique identity (property) and set the default value to 1.
  • For each subsequent access, we only need to add 1 to the corresponding attribute. The code test is shown in the figure below:

 @Test   //  Counter function implementation, such as the number of visits of a website in a day
 public void testCounter() {
     HashOperations forHash = redisTemplate.opsForHash();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
     String date = sdf.format(new Date());
     //  Judge whether the current date exists
     Boolean exist = forHash.hasKey("count", date);
     if (exist) {  //  If it exists, the number of visits increases
         forHash.increment("count", date, 1);
     } else {      //  For the first access, create and set the default value to 1
         forHash.put("count", date, 1);
     }
     Object count = forHash.get("count", date);
     System.out.println("Date:" + date + ",Number of visits:" + count);
 }

Many functions can also be evolved through the counter, such as the good / bad evaluation function of the shopping platform.

Good / bad comment function

It is also very simple to use Redis's hash to realize the functions of good and bad reviews of goods.

Since there are good and bad reviews for each product, it is initialized in advance. The code is shown in the figure:

flag is the parameter sent from the analog reception foreground. The positive comment is true and the negative comment is false.

 @Test   //  Realization of positive and negative comment function
 public void testAppraise() {
     HashOperations forHash = redisTemplate.opsForHash();
     //  Since good reviews and bad reviews are common to every product, they are initialized in advance
     forHash.put("goods_1", "good", 0);
     forHash.put("goods_1", "bad", 0);
     //  flag is the parameter sent from the analog reception foreground. The positive comment is true and the negative comment is false
     boolean flag = true;
     if (flag) { //  Praise
         forHash.increment("goods_1", "good", 1);
     } else {    //  negative comment
         forHash.increment("goods_1", "bad", 1);
     }
 }

This concludes the test of Redis's shopping cart, counter, good / bad rating and other functions.

Programming is very interesting. We can code all kinds of scenes in life. Flexible use can open the door to a new world. Welcome to study and discuss together~

Please indicate the source for reprint

If there is any mistake, please correct it

Thanks

Keywords: Java Database Redis Spring

Added by matt1 on Thu, 18 Nov 2021 07:25:13 +0200