Spring原生的~~~~
//CacheEvict 此注解描述方法时,表示此方法为一个更新缓存数据的切入点方法
//这里的menuCache表示要移除缓存数据的缓存对象,allEntries表示移除缓存所有数据,
//beforeInvocation表示目标方法执行结束以后移除缓存
//@Cacheable(value="menuCache") //这个注解描述的方法为一个切入点方法
@CacheEvict(value="menuCache",allEntries = true,beforeInvocation = false)//切入点方法
@Aspect
@Component
public class SysModuleCacheAspect {//Cache切面
/**
- 这里希望每个业务模块都有自己对应的cache
- 1)外层map的key对应着具体模块的cache
- 2)内层模块key为存储数据时使用的key*/
private Map<String,Map<String,Object>> cacheMap=new ConcurrentHashMap<>();
//@annotation 为注解方式的切入点表达式(由此注解描述的方法为切入点方法)
//@Pointcut("bean(sysDeptServiceImpl)") @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredCache)")//细粒度切入点表达式
private void doCache(){}
@Pointcut("@annotation(com.cy.pj.common.annotation.ClearCache)")//细粒度切入点表达式
private void doClearCache(){}
@AfterReturning("doClearCache()")
public void doAfterReturning(JoinPoint joinPoint)throws Exception{//method ok(success)
//Map<String,Object> cache=cacheMap.get("deptCache"); String cacheName=getCacheName(ClearCache.class,joinPoint);
System.out.println("cacheName----------"+cacheName);
Map<String,Object> cache=cacheMap.get(cacheName);
cache.clear();
}
private Method getTargetMethod(Class<? extends Annotation> annoClass, JoinPoint joinPoint)throws Exception{
Class<?> targetClass=joinPoint.getTarget().getClass();
//class com.cy.pj.sys.service.impl.SysDeptServiceImpl
MethodSignature ms=(MethodSignature) joinPoint.getSignature();
//List com.cy.pj.sys.service.SysDeptService.findObjects()
Method targetMethod= targetClass.getMethod(ms.getName(), ms.getParameterTypes());
//_public java.util.List com.cy.pj.sys.service.impl.SysDeptServiceImpl.findObjects()
return targetMethod;
}
private String getCacheName(Class<? extends Annotation> annoClass, JoinPoint joinPoint)throws Exception{
//1.获取目标方法
Method targetMethod=getTargetMethod(annoClass, joinPoint);
System.out.println("targetMethod_____+++"+annoClass);
//1.基于注解类型获取目标方法上的注解对象
Annotation annotation=targetMethod.getAnnotation(annoClass);
//@com.cy.pj.common.annotation.RequiredCache(name=deptCache, key=depts)
String cacheName=null;
//2.基于注解对象类型获取注解上的名字
if(annotation instanceof RequiredCache){
RequiredCache requiredCache= (RequiredCache)targetMethod.getAnnotation(annoClass);
cacheName=requiredCache.name();
}else if(annotation instanceof ClearCache){
ClearCache clearCache= (ClearCache) targetMethod.getAnnotation(annoClass);
cacheName=clearCache.name();
}
return cacheName;
}
private String getCacheKey(Class<? extends Annotation> annoClass, JoinPoint joinPoint)throws Exception{
//1.获取目标方法
Method targetMethod=getTargetMethod(annoClass, joinPoint);
//2.基于注解类型获取目标方法上的注解对象
Annotation annotation=targetMethod.getAnnotation(annoClass);
String key=null;
//3.基于注解对象类型获取注解上的名字
if(annotation instanceof RequiredCache){
RequiredCache requiredCache= (RequiredCache)targetMethod.getAnnotation(annoClass);
key=requiredCache.key();
}
return key;
}
@Around("doCache()") //@Around中方法的参数连接点的类型只能写ProceedingJoinPoint
public Object doCacheAround(ProceedingJoinPoint joinPoint)throws Throwable{
System.out.println("joinPoint+++++++"+joinPoint);
System.out.println("Get Data from cache");
Map<String, Object> cache=null;
synchronized (SysModuleCacheAspect.class) {
String cacheName = getCacheName(RequiredCache.class, joinPoint);
cache = cacheMap.get(cacheName);
if (cache == null) {
cache = new ConcurrentHashMap<>();//为对应key创建cache对象
cacheMap.put(cacheName, cache);
}
}
String key=getCacheKey(RequiredCache.class,joinPoint);
Object result=cache.get(key);//暂时先自己指定一个key的名字
if(result!=null)return result;
result=joinPoint.proceed();
System.out.println("Put data to cache");
cache.put(key, result);
return result;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。