注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ThreadLocalCache {
}
PointcutAdvisor
@Component
@Order(0)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
public class ThreadLocalCacheSupport implements PointcutAdvisor {
private static final Object NULL = new Object();
private ThreadLocal<Map<Object, Object>> threadLocal = new ThreadLocal<>();
@Getter
public Pointcut pointcut = new StaticMethodMatcherPointcut() {
@Override
public boolean matches(Method method, Class<?> targetClass) {
return method.getAnnotation(ThreadLocalCache.class) != null;
}
};
public boolean isExistInCache(Object thisObj, Method method, Object[] args) {
Object key = getCacheKey(thisObj, method, args);
Map<Object, Object> map = threadLocal.get();
if (map == null) {
return false;
} else {
return map.containsKey(key);
}
}
public Object getCacheKey(Object thisObj, Method method, Object[] args) {
return SimpleKeyGenerator.generateKey(thisObj, method, args);
}
@Getter
public MethodInterceptor advice = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object key = getCacheKey(invocation.getThis(), invocation.getMethod(), invocation.getArguments());
Map<Object, Object> map = threadLocal.get();
if(map == null) {
map = new HashMap<>();
threadLocal.set(map);
}
Object o = map.get(key);
if(o == NULL) {
return null;
} else if(o != null) {
return o;
}
Object result = invocation.proceed();
if(result == null) {
map.put(key, NULL);
} else {
map.put(key, result);
}
return result;
}
};
@Override
public boolean isPerInstance() {
return true;
}
public void removeAll() {
threadLocal.remove();
}
//移除所有的ThreadLocal
public static void destroy() {
try {
ApplicationContextUtil.getBean(ThreadLocalCacheSupport.class).removeAll();
}catch ( Exception e){
log.error("ThreadLocalSupport.destroy exception e = [{}]", e);
}
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。