Java 自定义注解结合 Aop 切面和本地缓存实现接口防重复请求提交
2022/2/8 14:42:30
本文主要是介绍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(); } |
这篇关于Java 自定义注解结合 Aop 切面和本地缓存实现接口防重复请求提交的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-19永别了,微服务架构!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?