参考 https://blog.csdn.net/weixin_30808575/article/details/95788091
参考 https://www.novelweb.cn/archives/120
import java.lang.annotation.*;
//作用于方法
@Target(ElementType.METHOD)
//整个运行阶段都可见
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface LocalLock {
String key() default “”;
/**
* 过期时间 由于用的guava 暂时就忽略这个属性 集成redis时需要用到
*/
int expire() default 5;
}
package com.spring.boot.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
@Aspect
@Configuration
public class LockMethodInterceptor {
//本地缓存
private static final Cache<String, Object> CACHES = CacheBuilder.newBuilder()
//最大1000个
.maximumSize(1000)
//设置写缓存后5秒钟过期
.expireAfterAccess(5, TimeUnit.SECONDS)
.build();
@Around("execution(public * *(..)) && @annotation(com.spring.boot.utils.LocalLock)")
public Object interceptor(ProceedingJoinPoint pjp) {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
LocalLock localLock = method.getAnnotation(LocalLock.class);
String key = getKey(localLock.key(), pjp.getArgs());
if (key != null || key != "") {
if (CACHES.getIfPresent(key) != null) {
throw new RuntimeException("请勿重复请求");
}
// 如果是第一次请求,就将 key 当前对象压入缓存中
CACHES.put(key, key);
}
try {
return pjp.proceed();
} catch (Throwable throwable) {
throw new RuntimeException("服务器异常");
} finally {
//CACHES.invalidate(key); 完成后移除key
}
}
private String getKey(String key, Object[] args) {
for (int i = 0; i < args.length; i++) {
key = key.replace("arg[" + i + "]", args[i].toString());
}
return key;
}
}
控制层
在接口上添加@LocalLock(key = “book:arg[0]”); 意味着会将arg[0]替换成第一个参数的值,生成后的新key将被缓存起来;
package com.spring.boot.controller;
import com.spring.boot.utils.LocalLock;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
@LocalLock(key=“book:arg[0]”)
@GetMapping
public String query(@RequestParam String token){
return "success - " + token;
}
}