How to realize redis current limiting Operation in java
The purpose of this article is to share with you the content about how to implement redis current limit operation in java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The specific operation is as follows.
Import required dependencies
UTF-8 1.8 5.0.2.RELEASE 1.6.6 1.2.12 5.1.6 3.4.5 org.aspectj aspectjweaver 1.6.8 org.springframework spring-aop ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring -web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-test ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} junit junit 4.12 Compile mysql mysql-connector-java ${mysql.version} javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.0 provided jstl jstl 1.2 log4j log4j ${log4j.version} org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-log4j12 ${slf4j.version} org.mybatis mybatis ${mybatis.version} org.mybatis mybatis-spring 1.3.0 c3p0 c3p0 0.9.1.2 jar compile org.springframework.data spring-data-redis 1.7.2.RELEASE redis.clients jedis 2.8.1
Write notes
@ Retention (RUNTIME) / / Runtime valid @ Target (ElementType.METHOD) / / public @ interface AccessLimit {int seconds (); / / time range (in seconds) int maxCount (); / / maximum number of visits within this time range}
Write interceptor
Public class AcessLimiitInterceptor implements HandlerInterceptor {/ / inject redisTemplate @ Autowired private RedisTemplate redisTemplate; @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) {/ / set the serialization mode of redisTemplate (must be set to this way, because incr is used) redisTemplate.setKeySerializer (new StringRedisSerializer ()); redisTemplate.setValueSerializer (new StringRedisSerializer ()) If (handler instanceof HandlerMethod) {/ / check whether there is @ AcessLimit annotation HandlerMethod hm= (HandlerMethod) handler; AccessLimit accessLimit=hm.getMethodAnnotation (AccessLimit.class) on this method; / / there is no @ AcessLimit annotation to prove the infinite flow operation and directly release if (accessLimit==null) {return true } / / get the parameter value of the comment int seconds=accessLimit.seconds (); / / time range int maxCount=accessLimit.maxCount (); / / maximum number of visits within the time range / / the path of the request String key=request.getRequestURI () / / the number of visits String countStr=redisTemplate.opsForValue () .get (key) within this time range; Integer count=null; / / if it is not the first visit, convert the number of visits to integer type if (countStructurally visited null) {count= Integer.valueOf (redisTemplate.opsForValue () .get (key)) } / / get the expiration time of the number of visits Long keySeconds=redisTemplate.getExpire (key); / / there is no access to if (count==null) {/ / the first visit within this time range. Set key to the access path, and the value is 1 redisTemplate.opsForValue () .set (key,1+ "") / / set expiration time redisTemplate.expire (key,600, TimeUnit.SECONDS);} else if (count)