Java Using Redis to Realize High Concurrency Counter Sample Code

  • 2021-08-28 20:08:23
  • OfStack

There are often scenarios where counters are needed in business requirements: for example, one mobile phone number limits sending five short messages in one day, one interface limits how many requests in one minute, one interface limits how many calls in one day, and so on. The above requirements can be easily realized by using Incr self-increasing command of Redis. Take one interface to limit the number of calls in one day as an example:


 /**
 *  Denial of service 
 * @return
 */
 private boolean denialOfService(String userId){
 long count=JedisUtil.setIncr(DateUtil.getDate()+"&"+userId+"&"+"queryCarViolation", 86400);
 if(count<=10){
  return false;
 }
 return true;
 }

    /**
 *  Inquiry violation 
 * @param plateNumber License plate 
 * @param vin  Frame number 
 * @param engineNo Engine 
 * @param request
 * @param response
 * @throws Exception
 */
 @RequestMapping("/queryCarViolationList.json")
 @AuthorizationApi
 public void queryCarViolationList(@CurrentToken Token token,String plateNumber,String vin,
    String engineNo,HttpServletRequest request,HttpServletResponse response) throws Exception {
   String userId=token.getUserId();
      // Limit exceeded, request intercepted 
   if(denialOfService(userId)){
  apiData(request, response, ReqJson.error(CarError.ONLY_5_TIMES_A_DAY_CAN_BE_FOUND));
  return;
   }
 // Without exceeding the limit, business logic... 
 }

Before each call interface, get the value of the counter after increment. If it is less than the limit, release and execute the following code. If it is greater than the limit, it is intercepted.

JedisUtil tool classes:


public class JedisUtil {
 protected final static Logger logger = Logger.getLogger(JedisUtil.class);
 private static JedisPool jedisPool;
 
 @Autowired(required = true)
 public void setJedisPool(JedisPool jedisPool) {
 JedisUtil.jedisPool = jedisPool;
 }
 /**
 *  Self-increment of the value of a key 
 * @author liboyi
 * @param key  Key 
 * @param cacheSeconds  Timeout, 0 Is not timeout 
 * @return
 */
 public static long setIncr(String key, int cacheSeconds) {
 long result = 0;
 Jedis jedis = null;
 try {
  jedis = jedisPool.getResource();
  result =jedis.incr(key);
  if (result<=1 && cacheSeconds != 0) {
  jedis.expire(key, cacheSeconds);
  }
  logger.debug("set "+ key + " = " + result);
 } catch (Exception e) {
  logger.warn("set "+ key + " = " + result);
 } finally {
  jedisPool.returnResource(jedis);
 }
 return result;
 }
}

Related articles: