package com.alvis.util;
/**
* @author ALvis
* @ctreate 2019/11/25
*/
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* AOP实现Redis缓存处理
*/
@Component
@Aspect
public class RedisAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisAspect.class);
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 拦截所有元注解RedisCache注解的方法
*/
@Pointcut("@annotation(com.alvis.util.RedisCacheC)")
public void pointcutMethod(){
}
/**
* 环绕处理,先从Redis里获取缓存,查询不到,就查询MySQL数据库,
* 然后再保存到Redis缓存里
* @param joinPoint
* @return
*/
@Around("pointcutMethod()")
public Object around(ProceedingJoinPoint joinPoint){
//前置:从Redis里获取缓存
//先获取目标方法参数
long startTime = System.currentTimeMillis();
String applId = "";
Object[] args = joinPoint.getArgs();
for (Object obj: args) {
applId = applId +","+ String.valueOf(obj);
}
applId = applId.substring(1, applId.length());
//获取目标方法所在类
String target = joinPoint.getTarget().toString();
String className = target.split("@")[0];
//获取目标方法的方法名称
String methodName = joinPoint.getSignature().getName();
//redis中key格式: applId:方法名称
String redisKey = applId + ":" + className + "." + methodName;
Object obj = redisTemplate.opsForValue().get(redisKey);
if(obj!=null){
LOGGER.info("**********从Redis中查到了数据**********");
LOGGER.info("Redis的KEY值:"+redisKey);
LOGGER.info("REDIS的VALUE值:"+obj.toString());
return obj;
}
long endTime = System.currentTimeMillis();
LOGGER.info("Redis缓存AOP处理所用时间:"+(endTime-startTime));
LOGGER.info("**********没有从Redis查到数据**********");
try{
obj = joinPoint.proceed();
}catch(Throwable e){
e.printStackTrace();
throw new RuntimeException(e);
}
LOGGER.info("**********开始从MySQL查询数据**********");
//后置:将数据库查到的数据保存到Redis
redisTemplate.opsForValue().set(redisKey, obj, 5, TimeUnit.MINUTES);
LOGGER.info("**********数据成功保存到Redis缓存!!!**********");
LOGGER.info("Redis的KEY值:"+redisKey);
LOGGER.info("REDIS的VALUE值:"+obj.toString());
return obj;
}
}
RedisAspect为切面核心类,可针对自己的项目来写存入key的规则,此例子为demo。
package com.alvis.util;
import java.lang.annotation.*;
/**
* @author ALvis
* @ctreate 2019/11/25
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedisCacheC {
/** 功能 */
String action() default "";
}
RedisCacheC为自定义注解,里面的参数只写了个功能,如果需要操作可以传入值在aop切面里写逻辑,这里就不多写了,跟一般的注解一样。
以上为核心类,下面就是应用了,很简单,要想用切面一定要用spring的动态代理才行,也就是说你加注解需要缓存的方法要依赖注入到需要的地方,如下。
@RedisCacheC
@Override
public EvalUser getUser(String name) {
System.out.println("---------1");
EvalUser user = new EvalUser();
user.setUserid("001");
user.setUsername(name);
user.setOrgId("111");
user.setLevel("222");
return user;
}
@RedisCacheC
@Override
public EvalUser getUser22(EvalUser evalUser) {
System.out.println("---------2");
EvalUser user = new EvalUser();
user.setUserid("001");
user.setUsername("nono");
user.setOrgId("111");
user.setLevel("222");
return user;
}
下面是controller
@Autowired
private IUserService iUserService;
EvalUser user22 = iUserService.getUser("alvis");
EvalUser user221 = iUserService.getUser22(user22);
iUserService.getUser22(user221);