In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 238
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.