Achieve polling of spike results on front-end pages

Introduction

  After the spike request is placed in the queue, due to the limited processing power of MySQL, it may take a while to complete the processing of the spike request; during this time, the front end will constantly ask the processing result of the spike request.

1. Define the polling function at the front end.

  Every 50ms, call the "/ miaosha / getresult" interface, passing in the parameters goodsId, when the spike is successful, it prompts whether to view the order page, when the spike fails, it displays the failure information, when the spike request

When it is still being processed, wait for 50ms and query again.

1  // Polling function: Check every 50ms to see if the spike succeeds 
2      function getResult (goodsId) {
 3          g_showLoading ();
 4          $ .ajax ({
 5              url: "/ miaosha / getresult" ,
 6              type: "GET" ,
 7              data: {
 8                  goodsId: $ ("# goodsId" ) .val (),
 9              },
 10              success: function (data) {
 11                  var result = data.data;
 12                  if (result <0 ) {
 13                      layer. msg ("Sorry, the spike failed" );
14                  } else  if (result == 0 ) {
 15                      setTimeout (function () {
 16                          getResult (goodsId);
 17                      }, 50 )
 18                  } else {
 19                      layer.confirm ("Congratulations, the spike is successful. Check the order?", {btn: ["OK", "Cancel" ]},
 20                                      function () {
 21                                          window.location.href = "/ order_detail.htm? orderId =" + result;
 22                                      },
 23                                     function () {
24                                         layer.closeAll();
25 
26                                     });
27                 }
28 
29             },
30             error:function () {
31                 layer.msg(data.msg);
32             }
33         });
34 
35     }

2. The back-end getResult method will return the processing result of spike

  Success: return the order id; failure: return -1, indicating that there is no stock; in processing: return 0.

  

1  / * 
2      * Polling function: view the result of spike
 3      * * / 
4      @AccessLimit (seconds = 5, maxCount = 5 )
 5      @RequestMapping (value = "/ getresult", method = RequestMethod.GET)
 6      @ResponseBody
 7      public Result <Long> getResult (Model model, MiaoshaUser user, @ RequestParam ("goodsId") long goodsId) {
 8          // Judge the current spike status 
9          long result = miaoshaService.getMiaoshaResult (user.getId (), goodsId);
 10  11          return Result.success (result);
 12      }

3. The getMiaoshaResult () method in miaoshaService.java is the method to really get the result of spike

  There are two parameters for this method, goodsId and userId. These two parameters will be used for querying.

  Two judgments are made in this method, the first time, whether the spike order corresponding to goodsId + userId exists, and the order number is returned; if it does not exist, the second time

  The second time, the situation of reducing inventory is judged. If the inventory reduction fails, the spike will be returned, and the other cases will be considered as being processed.

1  // Determine the status of the spike during polling 
2      public  long getMiaoshaResult (Long userId, long goodsId) { 3          // 1.Determine
 whether the spike is successful 
4          OrderInfo orderInfo = orderInfoService.getByUserIdGoodsId (userId, goodsId);
 5          if (orderInfo! = null ) {
 6              return orderInfo.getId ();                                                                                    // The order ID 
7          }
 8          // If the success is successful, determine if the failure is successful or in progress 
9          if (! getReduceResult (goodsId)) {
 10             return -1;         // No stock, spike kill 
11          } else  return 0;        // In stock, spike kill 
12      }

  In the second judgment, you need to use the getResult () function, which is a function to obtain the reduced inventory result, and there is a corresponding setResult () function to save the reduced inventory result.

 

1 private void setReduceResult(long goodsId, boolean reduceResult) {
2         redisService.set(ReduceResultPrefix.getReduceResult,""+goodsId,reduceResult);
3     }
4 
5     private boolean getReduceResult(long goodsId) {
6         return  redisService.exists(ReduceResultPrefix.getReduceResult,""+goodsId);
7     }

  Call the setReduceResult method after the inventory reduction operation to cache the results of the inventory reduction in redis.

1  // Three steps: reduce inventory, place order, write spike order 
2      @Transactional
 3      public OrderInfo miaoSha ( long useId, long goodsId) { 4                                                                                                                          // Reduce
 inventory, and set the result of reducing inventory 
5          boolean reduceResult = miaoshaGoodsService. reduceStock (goodsId);
 6          miaoshaGoodsService.reduceFMStock (goodsId);
 7          setReduceResult (goodsId, reduceResult);
 8          if (! reduceResult) {
 9              return  null ;
 10          }

  Among them, miaoshaGoodsService.reduceStock (goodsId); will call the update operation of ORM framework to reduce inventory;

  The update operation will return the number of rows affected. If the return value is 1, it means that the inventory reduction was successful; the return value is 0, which means that the inventory reduction failed.

  In the miaoshaGoodsService.reduceStock () method, 0 and 1 will be converted to false and true as the return value, and then assigned to the reduceResult variable.

  Then cache the reduceResult variable into redis.

 

Guess you like

Origin www.cnblogs.com/deijiawoyu/p/12693824.html