基于装饰模式+ aop+ jvm-map + redis +注解设计一级 +二级缓存框架

缓存描述

1、多级缓存基本概念

在实际开发项目,为了减少数据库的访问压力,我们都会将数据缓存到内存中
比如:Redis(分布式缓存)、EHCHE(JVM内置缓存).
例如在早起中,项目比较小可能不会使用Redis做为缓存,使用JVM内置的缓存框架,
项目比较大的时候开始采用Redis分布式缓存框架,这时候需要设计一级与二级缓存。

2、装饰模式基本的概念

不改变原有代码的基础之上,新增附加功能
在这里插入图片描述

一、 代码结构

在这里插入图片描述

二、具体代码

1、自定义注解

/**
 * 自定义缓存注解
 */
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExtXjCache {
}

2、自定义注解的Aop代理类

/**
 * 自定义注解Aop
 */
@Aspect
@Component
@Slf4j
public class XjCacheAop {

    // 二级缓存执行对象
    @Autowired
    private XjCache xjCache;

    /**
     * 使用Aop拦截我们的方法上是否使用缓存注解
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around(value = "@annotation(com.xijia.config.aop.ExtXjCache)")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        // 获取目标方法
        Method targetMethod = methodSignature.getMethod();
        // 拼接缓存的key = 方法名 + 参数类型 + 查询参数值
        String key = targetMethod.getName() + Arrays.toString(targetMethod.getParameterTypes()) + Arrays.toString(joinPoint.getArgs());
        // 执行二级缓存,传入 joinPoint 对象; 当没有获取到缓存数据时,在执行目标对象 joinPoint.proceed(),如果存在缓存数据,则提前返回,不查询数据库
        Object result = xjCache.getCacheEntity(key, targetMethod.getReturnType(), joinPoint);
        return result;
    }
}

3、一级缓存

容器(jvm-Map)

/**
 * jvm 缓存容器类
 */
public class JvmMapCacheUtils {
    /**
     * 缓存容器
     */
    private static Map<String, String> caches = new ConcurrentHashMap<>();

    public static  <T> T getEntity(String key, Class<T> t) {
        // 缓存存放对象的情况
        String json = caches.get(key);
        return JSONObject.parseObject(json, t);
    }

    public static void putEntity(String key, Object o) {
        String json = JSONObject.toJSONString(o);
        caches.put(key, json);
    }

}

接口

public interface ComponentCache {

    /**
     * 根据key查询缓存数据
     *
     * @param
     * @return
     */
    <T> T getCacheEntity(String key,Class<T> t, ProceedingJoinPoint joinPoint);
}

实现

/**
  * 一级缓存(jvm缓存)
  * @author wangsong
  * @mail  [email protected]
  * @date  2020/8/31 0031 23:00
  * @version 1.0.0
  */
@Component
public class JvmComponentCache implements ComponentCache {

    /**
     *
     * @param key 缓存key
     * @param t 返回数据类型
     * @param joinPoint aop传入的对象
     * @param <T>
     * @return
     */
    @Override
    public <T> T getCacheEntity(String key, Class<T> t, ProceedingJoinPoint joinPoint) {
        // 查询一级的Jvm缓存, 如果存在, 直接返回
        T jvmUser = JvmMapCacheUtils.getEntity(key,t);
        if (jvmUser != null) {
            return (T) jvmUser;
        }
        // 一级缓存不存在, 通过aop 传入的joinPoint 执行目标对象方法, 由目标对象去查询数据库获取数据
        try {
            Object resultDb = joinPoint.proceed();
            // 数据库DB有的情况 将该内容缓存到当前Jvm中
            JvmMapCacheUtils.putEntity(key, resultDb);
            return (T) resultDb;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }
}

4、二级缓存

二级缓存,使用装饰模式对一级缓存做增强, 继承一级缓存接口 ComponentCache

抽象类接口

public interface AbstractDecorate extends ComponentCache {
}

二级缓存装饰类

/**
 * 二级缓存装饰类
 * extends JvmComponentCache 可使用 super 来查询一级缓存数据
 */
@Component
public class RedisDecorate extends JvmComponentCache implements AbstractDecorate {
    @Autowired
    private RedisUtils redisUtils;

//    @Autowired
//    private JvmComponentCache jvmComponentCache;

    /**
     *
     * @param key 缓存对象key
     * @param t 返回数据类型
     * @param joinPoint aop的传入的执行对象
     * @param <T>
     * @return
     */
    @Override
    public <T> T getCacheEntity(String key, Class<T> t, ProceedingJoinPoint joinPoint) {
        // 查询二级缓存, 如果存在,直接返回
        T redisUser = redisUtils.getEntity(key, t);
        if (redisUser != null) {
            return (T) redisUser;
        }
        // 二级缓存不存在,查询一级缓存, 并把一级缓存返回的数据放入二级缓存中
        T jvmUser = super.getCacheEntity(key, t, joinPoint);
        if (jvmUser != null) {
            redisUtils.putEntity(key, jvmUser);
            return (T) jvmUser;
        }
        // 没有查询到数据
        return null;
    }
}

对外暴露执行对象

@Component
public class XjCache {

    @Autowired
    private RedisDecorate redisDecorate;

    public <T> T getCacheEntity(String key, Class<T> t, ProceedingJoinPoint joinPoint) {
        return redisDecorate.getCacheEntity(key, t, joinPoint);
    }
}

三、使用缓存 /描述

任意方法加上 自定义缓存注解

  • 执行方法 aop 拦截到带有注解的方法
  • 获取方法名,方法参数类型,方法查询的参数 == 拼成缓存的 key
  • 在aop 调用二级缓存对象,判断是否存在,不存在调用一级缓存对象
  • 一级缓存对象判断是否存在,不存在执行 aop,调用目标方法,目标方法查询数据库数据返回保存到一级缓存和二级缓存中

在这里插入图片描述

  • 以上部分内容来自于蚂蚁课堂

  • 个人开源项目(通用后台管理系统)–> https://gitee.com/wslxm/spring-boot-plus2 , 喜欢的可以看看

  • 本文到此结束,如果觉得有用,动动小手点赞或关注一下呗,将不定时持续更新更多的内容…,感谢大家的观看!

猜你喜欢

转载自blog.csdn.net/qq_41463655/article/details/108332102