精讲23种设计模式-004:基于装饰模式设计多级缓存框架

1 基于装饰模式手写多级缓存框架演示

课程内容

  1. 如何理解多级缓存框架设计
  2. 装饰模式与代理模式之间的区别
  3. 基于装饰设计多级缓存框架
  4. 自定义缓存注解,轻松搞定多级缓存问题

2 一级与二级缓存基本的概念

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

缓存机制
JVM内置缓存:将数据缓存到当前JVM中
缺陷:占用当前JVM内存空间,可能造成内存溢出问题;集群很难保证各个节点之间数据同步问题。
举例:EhCache,OsCache底层原理采用HashMap实现 淘汰策略
分布式缓存Redis:数据能够共享

装饰模式概念:不改变原有的代码实现增强。Mybatis、hibernate二级缓存都属于开发者自己去实现扩展的功能。

装饰模式与代理模式区别:
代理模式对目标对象(目标方法)实现增强;
装饰模式对装饰对象实现增强,不能改变原有代码。

3 手写模拟一级与二级缓存基本概念

基于HashMap手写Jvm内置缓存

public class JvmMapCacheUtils {

    /**
     * 缓存容器
     */
    private static Map<String, String> caches = new ConcurrentHashMap<>();

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

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

模拟一级与二级缓存概念

@RestController
@Slf4j
public class MemberService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("/getUser")
    public UserEntity getUser(Integer userId) {
        // 一级和二级缓存
        // 方法名称+参数名称+参数内容组成key
        String key = "getUser(Integer)" + userId;

        // 查询二级缓存
        UserEntity redisUser = redisUtils.getEntity(key, UserEntity.class);
        if (redisUser != null) {
            return redisUser;
        }
        // 查询一级缓存(JVM内置缓存)
        UserEntity jvmUser = JvmMapCacheUtils.getEntity(key, UserEntity.class);
        if (jvmUser != null) {
            // 将该缓存数据放入到二级缓存中
            redisUtils.putEntity(key, jvmUser);
            return jvmUser;
        }
        // 查询db
        UserEntity dbUser = userMapper.findByUser(userId);
        if (dbUser == null) {
            return null;
        }
        // 数据库DB有的情况下,将该内容缓存到当前JVM中
        JvmMapCacheUtils.put(key, dbUser);
        return dbUser;
    }
}

运行结果:
请添加图片描述

4 装饰模式基本架构设计原理

装饰模式基本概念
在不改变原有代码的基础之上,新增附加功能
请添加图片描述
装饰模式应用场景
多级缓存设计、mybatis中一级与二级缓存、IO流

装饰者模式定义
(1)抽象组件:定义一个抽象接口,来规范准备附加功能的类
(2)具体组件:将要被附加功能的类,实现抽象构件角色接口
(3)抽象装饰者:持有对具体构件角色的引用并定义与抽象构件角色一致的接口
(4)具体装饰:实现抽象装饰者角色,负责对具体构件添加额外功能。

5 定义早期装饰模式一级缓存

public interface ComponentCache {

    /**
     * 根据key查询缓存数据
     *
     * @param <T>
     * @return
     */
    <T> T getCacheEntity(String key);
}
@Component
public class JvmComponentCache implements ComponentCache {

    @Autowired
    private UserMapper userMapper;

    @Override
    public <T> T getCacheEntity(String key) {
        // 先查询我们的一级缓存(Jvm内置)
        UserEntity jvmUser = JvmMapCacheUtils.getEntity(key, UserEntity.class);
        if (jvmUser != null) {
            return (T) jvmUser;
        }
        // 查询db 通过aop直接获取到目标对象方法
        UserEntity dbUser = userMapper.findByUser(1);
        if (dbUser == null) {
            return null;
        }
        // 数据库DB有的情况下,将该内容缓存到当前JVM中
        JvmMapCacheUtils.put(key, dbUser);
        return (T) dbUser;
    }
}

6 基于装饰模式重构设计多级缓存

public interface AbstractDecorate extends ComponentCache{
}
/**
 * @Name RedisDecorate
 * @Author 枫火
 * @Date 2021/10/3 17:07
 * @Description 这里实现AbstractCache而不是ComponentCache是为了区分装饰类(装饰需求)和业务逻辑类(基本功能)。
 * 这里RedisDecorate是通过Component注入到spring容器中,所以extends JvmComponentCache可以获取到JvmComponentCache的UserMapper,如果是new出的就不能获取
 **/
@Component
public class RedisDecorate extends JvmComponentCache implements AbstractDecorate {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public <T> T getCacheEntity(String key) {
        // 查询二级缓存
        UserEntity redisUser = redisUtils.getEntity(key, UserEntity.class);
        if (redisUser != null) {
            return (T) redisUser;
        }
        // 查询一级缓存(JVM内置缓存)
        UserEntity jvmUser = super.getCacheEntity(key);
        if (jvmUser == null) {
            return null;
        }
        // 将该缓存数据放入到二级缓存中
        redisUtils.putEntity(key, jvmUser);
        return (T) jvmUser;
    }
}
@Component
public class MayiktCache {

    @Autowired
    private RedisDecorate redisDecorate;

    public <T> T getCacheEntity(String key) {
        return redisDecorate.getCacheEntity(key);
    }
}
@RestController
@Slf4j
public class MemberService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MayiktCache mayiktCache;

    @RequestMapping("/getUser")
    public UserEntity getUser(Integer userId) {
        String key = "getUser(Integer)" + userId;
        return mayiktCache.getCacheEntity(key);
    }
}

这里查询db中的参数是暂时写死的,主要体现多级缓存实现思路。

7 基于Aop拦截自定义缓存注解

/**
 * 自定义缓存注解
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExtMeiteCache {
}
@Aspect
@Component
@Slf4j
public class ExtAsyncAop {

    /**
     * 使用Aop拦截方法上是否有使用缓存注解
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around(value = "@annotation(com.mayikt.aop.ExtMeiteCache)")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // 目标方法
        log.info(">>>目标方法开始执行..");
        Object result = joinPoint.proceed();
        log.info(">>>目标方法结束执行..");
        return result;
    }
}
@RestController
@Slf4j
public class MemberService {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/getUser")
    @ExtMeiteCache
    public UserEntity getUser(Integer userId) {
        return userMapper.findByUser(userId);
    }
}

运行结果:
请添加图片描述

8 使用Aop回调形式传递目标方法&使用泛型接收目标方法类型

public interface ComponentCache {

    /**
     * 根据key查询缓存数据
     *
     * @param <T>
     * @return
     */
    <T> T getCacheEntity(String key, Class<T> t, ProceedingJoinPoint joinPoint);
}
@Component
public class JvmComponentCache implements ComponentCache {

    @Autowired
    private UserMapper userMapper;

    @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;
        }
        try {
            // 执行目标对象的目标方法,即MemberService的getUser方法
            Object resultDb = joinPoint.proceed();
            JvmMapCacheUtils.put(key, resultDb);
            return (T) resultDb;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }
}
@Component
public class RedisDecorate extends JvmComponentCache implements AbstractDecorate {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public <T> T getCacheEntity(String key, Class<T> t, ProceedingJoinPoint joinPoint) {
        // 查询二级缓存
        T redisUser = redisUtils.getEntity(key, t);
        if (redisUser != null) {
            return (T) redisUser;
        }
        // 查询一级缓存(JVM内置缓存)
        T jvmUser = super.getCacheEntity(key, t, joinPoint);
        if (jvmUser == null) {
            return null;
        }
        // 将该缓存数据放入到二级缓存中
        redisUtils.putEntity(key, jvmUser);
        return (T) jvmUser;
    }
}
@Component
public class MayiktCache {

    @Autowired
    private RedisDecorate redisDecorate;

    public <T> T getCacheEntity(String key, Class<T> t, ProceedingJoinPoint joinPoint) {
        return redisDecorate.getCacheEntity(key, t, joinPoint);
    }
}
@Aspect
@Component
@Slf4j
public class ExtAsyncAop {

    @Autowired
    private MayiktCache mayiktCache;

    /**
     * 使用Aop拦截方法上是否有使用缓存注解
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around(value = "@annotation(com.mayikt.aop.ExtMeiteCache)")
    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());
        Object result = mayiktCache.getCacheEntity(key, targetMethod.getReturnType(), joinPoint);
        return result;
    }
}

测试结果:
请添加图片描述
源码下载地址(mayikt_designPattern_4.rar):
链接:https://pan.baidu.com/s/1wWKZN1MbXICZVW1Vxtwe6A
提取码:fire

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值