您的位置:首页 > 科技 > 能源 > 福田瑞沃轻卡_2023西安疫情最新消息今天_青岛网站seo推广_windows优化大师官方免费

福田瑞沃轻卡_2023西安疫情最新消息今天_青岛网站seo推广_windows优化大师官方免费

2025/3/7 2:46:55 来源:https://blog.csdn.net/qq_34874784/article/details/143465815  浏览:    关键词:福田瑞沃轻卡_2023西安疫情最新消息今天_青岛网站seo推广_windows优化大师官方免费
福田瑞沃轻卡_2023西安疫情最新消息今天_青岛网站seo推广_windows优化大师官方免费

在Spring Boot应用中,可以使用Google Guava缓存来实现防重复提交功能。Guava提供了强大的缓存机制,可以方便地实现对请求的去重处理。。

1. 添加依赖

首先,在你的pom.xml文件中添加Guava的依赖:

<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>31.0.1-jre</version>
</dependency>
 

2. 创建缓存配置类

创建一个配置类来初始化Guava缓存:

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.concurrent.TimeUnit;@Configuration
public class CacheConfig {@Beanpublic Cache<String, String> requestCache() {return CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES) // 设置缓存过期时间.build();}
}
 

3. 创建防重复提交拦截器

接下来,创建一个拦截器来检查请求是否重复:

import com.google.common.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@Component
public class DuplicateSubmissionInterceptor implements HandlerInterceptor {@Autowiredprivate Cache<String, String> requestCache;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String key = generateKey(request);if (key != null && requestCache.getIfPresent(key) != null) {response.setStatus(HttpServletResponse.SC_CONFLICT); // 409 Conflictreturn false;} else {requestCache.put(key, "");return true;}}private String generateKey(HttpServletRequest request) {// 根据请求生成唯一键,例如可以根据URL和参数生成MD5值String url = request.getRequestURI();String queryString = request.getQueryString();return url + (queryString != null ? "?" + queryString : "");}
}

 

4. 注册拦截器

最后,将拦截器注册到Spring MVC中:

mport org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebConfig implements WebMvcConfigurer {@Autowiredprivate DuplicateSubmissionInterceptor duplicateSubmissionInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(duplicateSubmissionInterceptor).addPathPatterns("/**"); // 对所有请求进行拦截}
}
 

总结

通过以上步骤,已经成功实现了一个基于Guava缓存的防重复提交功能。利用Guava缓存的高效性和简洁性,能够有效地防止短时间内的重复请求。你可以根据实际需求调整缓存的过期时间和生成唯一键的逻辑。

推荐阅读

nginx代理udp协议

springboot 自定义注解实现redis 秒级 缓存

springboot rocketmq 一秒拉取一次消息,批量消费消息

java hashmap 面试题

springboot与tio-websocket自定义集群模式

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com