1 基于装饰模式手写多级缓存框架演示
课程内容
- 如何理解多级缓存框架设计
- 装饰模式与代理模式之间的区别
- 装饰模式如何在Mybatis、IO流运用
- 基于装饰设计多级缓存框架
- 自定义缓存注解,轻松搞定多级缓存问题
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) {
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) {
String key = "getUser(Integer)" + userId;
UserEntity redisUser = redisUtils.getEntity(key, UserEntity.class);
if (redisUser != null) {
return redisUser;
}
UserEntity jvmUser = JvmMapCacheUtils.getEntity(key, UserEntity.class);
if (jvmUser != null) {
redisUtils.putEntity(key, jvmUser);
return jvmUser;
}
UserEntity dbUser = userMapper.findByUser(userId);
if (dbUser == null) {
return null;
}
JvmMapCacheUtils.put(key, dbUser);
return dbUser;
}
}
运行结果:
4 装饰模式基本架构设计原理
装饰模式基本概念 在不改变原有代码的基础之上,新增附加功能 装饰模式应用场景 多级缓存设计、mybatis中一级与二级缓存、IO流
装饰者模式定义 (1)抽象组件:定义一个抽象接口,来规范准备附加功能的类 (2)具体组件:将要被附加功能的类,实现抽象构件角色接口 (3)抽象装饰者:持有对具体构件角色的引用并定义与抽象构件角色一致的接口 (4)具体装饰:实现抽象装饰者角色,负责对具体构件添加额外功能。
5 定义早期装饰模式一级缓存
public interface ComponentCache {
<T> T getCacheEntity(String key);
}
@Component
public class JvmComponentCache implements ComponentCache {
@Autowired
private UserMapper userMapper;
@Override
public <T> T getCacheEntity(String key) {
UserEntity jvmUser = JvmMapCacheUtils.getEntity(key, UserEntity.class);
if (jvmUser != null) {
return (T) jvmUser;
}
UserEntity dbUser = userMapper.findByUser(1);
if (dbUser == null) {
return null;
}
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> T getCacheEntity(String key) {
UserEntity redisUser = redisUtils.getEntity(key, UserEntity.class);
if (redisUser != null) {
return (T) redisUser;
}
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 {
@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> 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) {
T jvmUser = JvmMapCacheUtils.getEntity(key, t);
if (jvmUser != null) {
return (T) jvmUser;
}
try {
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;
}
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;
@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();
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
|