Java 自定义注解结合 Aop 切面和本地缓存实现接口防重复请求提交
Aop 切面和本地缓存实现接口防重复请求提交
自定义注解类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 重复请求
*
* @Author:
* @Date:
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatRequest {
}
|
缓存配置类
这边本地缓存用的是google guava cache,缓存有效期是3秒。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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;
/**
* @Author:
* @Date:
*/
@Configuration
public class CacheConfig {
@Bean
public Cache repeatCache(){
final Cache<Object, String> repeatCache = CacheBuilder.newBuilder()
// cache的初始大小
.initialCapacity(20)
// 并发数
.concurrencyLevel(10)
// cache中的数据在写入之后的存活时间为3秒
.expireAfterWrite(3, TimeUnit.SECONDS)
.build();
return repeatCache;
}
}
|
切面类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import cn.hutool.core.util.ArrayUtil;
import com.google.common.cache.Cache;
import com.ls.uem.emer.command.common.exception.CommandErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 防重复请求
*
* @Author:
* @Date:
*/
@Aspect
@Slf4j
@Component
public class NoRepeatRequestAspect {
@Autowired
private Cache<Object, Object> repeatCache;
@Pointcut("@annotation(com.sww.annotation.NoRepeatRequest)")
public void noRepeat() {
}
@Around("noRepeat()")
public Object repeatRequestCheck(ProceedingJoinPoint joinPoint) throws Throwable {
Object result;
Object[] args = joinPoint.getArgs();
if (ArrayUtil.isNotEmpty(args)){
Object key = args[0];
log.info("防重复请求缓存实时数量:{}", repeatCache.size());
if (repeatCache.getIfPresent(key) == null){
repeatCache.put(key, System.currentTimeMillis());
result = joinPoint.proceed();
} else{
throw new BizServiceException(ErrorCode.REPEAT_REQUEST.getDesc());
}
} else{
result = joinPoint.proceed();
}
return result;
}
}
|
接口
1
2
3
4
5
6
7
8
9
10
11
|
/**
*
* @param vo
* @return
*/
@PostMapping("/tttt")
@NoRepeatRequest
public JsonResult tttt(@RequestBody @Valid TestVO vo) {
commandService.save(vo);
return JsonResult.success();
}
|
posted on 2022-02-08 14:27 ExplorerMan 阅读(352) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端