栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

004:基于装饰模式设计多级缓存框架
    • 1 基于装饰模式手写多级缓存框架演示
    • 2 一级与二级缓存基本的概念
    • 3 手写模拟一级与二级缓存基本概念
    • 4 装饰模式基本架构设计原理
    • 5 定义早期装饰模式一级缓存
    • 6 基于装饰模式重构设计多级缓存
    • 7 基于Aop拦截自定义缓存注解
    • 8 使用Aop回调形式传递目标方法&使用泛型接收目标方法类型

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

课程内容

  1. 如何理解多级缓存框架设计
  2. 装饰模式与代理模式之间的区别
  3. 装饰模式如何在Mybatis、IO流运用
  4. 基于装饰设计多级缓存框架
  5. 自定义缓存注解,轻松搞定多级缓存问题
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 caches = new ConcurrentHashMap<>();

    public static  T getEntity(String key, Class 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 {

    
     T getCacheEntity(String key);
}
@Component
public class JvmComponentCache implements ComponentCache {

    @Autowired
    private UserMapper userMapper;

    @Override
    public  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{
}
@Component
public class RedisDecorate extends JvmComponentCache implements AbstractDecorate {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public  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 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 {

    
    @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 {

    
     T getCacheEntity(String key, Class t, ProceedingJoinPoint joinPoint);
}
@Component
public class JvmComponentCache implements ComponentCache {

    @Autowired
    private UserMapper userMapper;

    @Override
    public  T getCacheEntity(String key, Class 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 getCacheEntity(String key, Class 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 getCacheEntity(String key, Class t, ProceedingJoinPoint joinPoint) {
        return redisDecorate.getCacheEntity(key, t, joinPoint);
    }
}
@Aspect
@Component
@Slf4j
public class ExtAsyncAop {

    @Autowired
    private MayiktCache mayiktCache;

    
    @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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/287058.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号