3、关联缓存刷新实现
打开二级缓存,本地项目使用 MyBatis Plus
mybatis-plus.configuration.cache-enabled=true
主要用到自定义注解CacheRelations,自定义缓存实现RelativeCache和缓存上下文RelativeCacheContext。
注解CacheRelations,使用时需标注在对应mapper上
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface CacheRelations { // from中mapper class对应的缓存更新时,需要更新当前注解标注mapper的缓存 Class<?>[] from() default {}; // 当前注解标注mapper的缓存更新时,需要更新to中mapper class对应的缓存 Class<?>[] to() default {}; }
自定义缓存RelativeCache实现 MyBatis Cache 接口
public class RelativeCache implements Cache { private Map<Object, Object> CACHE_MAP = new ConcurrentHashMap<>(); private List<RelativeCache> relations = new ArrayList<>(); private ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true); private String id; private Class<?> mapperClass; private boolean clearing; public RelativeCache(String id) throws Exception { this.id = id; this.mapperClass = Class.forName(id); RelativeCacheContext.putCache(mapperClass, this); loadRelations(); } @Override public String getId() { return id; } @Override public void putObject(Object key, Object value) { CACHE_MAP.put(key, value); } @Override public Object getObject(Object key) { return CACHE_MAP.get(key); } @Override public Object removeObject(Object key) { return CACHE_MAP.remove(key); } @Override public void clear() { ReadWriteLock readWriteLock = getReadWriteLock(); Lock lock = readWriteLock.writeLock(); lock.lock(); try { // 判断 当前缓存是否正在清空,如果正在清空,取消本次操作 // 避免缓存出现 循环 relation,造成递归无终止,调用栈溢出 if (clearing) { return; } clearing = true; try { CACHE_MAP.clear(); relations.forEach(RelativeCache::clear); } finally { clearing = false; } } finally { lock.unlock(); } } @Override public int getSize() { return CACHE_MAP.size(); } @Override public ReadWriteLock getReadWriteLock() { return readWriteLock; } public void addRelation(RelativeCache relation) { if (relations.contains(relation)){ return; } relations.add(relation); } void loadRelations() { // 加载 其他缓存更新时 需要更新此缓存的 caches // 将 此缓存 加入至这些 caches 的 relations 中 List<RelativeCache> to = UN_LOAD_TO_RELATIVE_CACHES_MAP.get(mapperClass); if (to != null) { to.forEach(relativeCache -> this.addRelation(relativeCache)); } // 加载 此缓存更新时 需要更新的一些缓存 caches // 将这些缓存 caches 加入 至 此缓存 relations 中 List<RelativeCache> from = UN_LOAD_FROM_RELATIVE_CACHES_MAP.get(mapperClass); if (from != null) { from.forEach(relativeCache -> relativeCache.addRelation(this)); } CacheRelations annotation = AnnotationUtils.findAnnotation(mapperClass, CacheRelations.class); if (annotation == null) { return; } Class<?>[] toMappers = annotation.to(); Class<?>[] fromMappers = annotation.from(); if (toMappers != null && toMappers.length > 0) { for (Class c : toMappers) { RelativeCache relativeCache = MAPPER_CACHE_MAP.get(c); if (relativeCache != null) { // 将找到的缓存添加到当前缓存的relations中 this.addRelation(relativeCache); } else { // 如果找不到 to cache,证明to cache还未加载,这时需将对应关系存放到 UN_LOAD_FROM_RELATIVE_CACHES_MAP // 也就是说 c 对应的 cache 需要 在 当前缓存更新时 进行更新 List<RelativeCache> relativeCaches = UN_LOAD_FROM_RELATIVE_CACHES_MAP.putIfAbsent(c, new ArrayList<RelativeCache>()); relativeCaches.add(this); } } } if (fromMappers != null && fromMappers.length > 0) { for (Class c : fromMappers) { RelativeCache relativeCache = MAPPER_CACHE_MAP.get(c); if (relativeCache != null) { // 将找到的缓存添加到当前缓存的relations中 relativeCache.addRelation(this); } else { // 如果找不到 from cache,证明from cache还未加载,这时需将对应关系存放到 UN_LOAD_TO_RELATIVE_CACHES_MAP // 也就是说 c 对应的 cache 更新时需要更新当前缓存 List<RelativeCache> relativeCaches = UN_LOAD_TO_RELATIVE_CACHES_MAP.putIfAbsent(c, new ArrayList<RelativeCache>()); relativeCaches.add(this); } } } } }
缓存上下文RelativeCacheContext
public class RelativeCacheContext { // 存储全量缓存的映射关系 public static final Map<Class<?>, RelativeCache> MAPPER_CACHE_MAP = new ConcurrentHashMap<>(); // 存储 Mapper 对应缓存 需要to更新缓存,但是此时 Mapper 对应缓存还未加载 // 也就是 Class<?> 对应的缓存更新时,需要更新 List<RelativeCache> 中的缓存 public static final Map<Class<?>, List<RelativeCache>> UN_LOAD_TO_RELATIVE_CACHES_MAP = new ConcurrentHashMap<>(); // 存储 Mapper 对应缓存 需要from更新缓存,但是在 加载 Mapper 缓存时,这些缓存还未加载 // 也就是 List<RelativeCache> 中的缓存更新时,需要更新 Class<?> 对应的缓存 public static final Map<Class<?>, List<RelativeCache>> UN_LOAD_FROM_RELATIVE_CACHES_MAP = new ConcurrentHashMap<>(); public static void putCache(Class<?> clazz, RelativeCache cache) { MAPPER_CACHE_MAP.put(clazz, cache); } public static void getCache(Class<?> clazz) { MAPPER_CACHE_MAP.get(clazz); } }
使用方式
UserMapper.java
@Repository @CacheNamespace(implementation = RelativeCache.class, eviction = RelativeCache.class, flushInterval = 30 * 60 * 1000) @CacheRelations(from = OrganizationMapper.class) public interface UserMapper extends BaseMapper<UserEntity> { UserInfo queryUserInfo(@Param("userId") String userId); }
queryUserInfo是xml实现的接口,所以需要在对应xml中配置,不然查询结果不会被缓存化。如果接口为 BaseMapper实现,查询结果会自动缓存化。
UserMapper.xml
<mapper namespace="com.mars.system.dao.UserMapper"> <cache-ref namespace="com.mars.system.dao.UserMapper"/> <select id="queryUserInfo" resultType="com.mars.system.model.UserInfo"> select u.*, o.name org_name from user u left join organization o on u.org_id = o.id where u.id = #{userId} </select> </mapper>
OrganizationMapper.java
@Repository @CacheNamespace(implementation = RelativeCache.class, eviction = RelativeCache.class, flushInterval = 30 * 60 * 1000) public interface OrganizationMapper extends BaseMapper<OrganizationEntity> { }
CacheNamespace中flushInterval 在默认情况下是无效的,也就是说缓存并不会定时清理。ScheduledCache是对flushInterval 功能的实现,MyBatis 的缓存体系是用装饰器进行功能扩展的,所以,如果需要定时刷新,需要使用ScheduledCache给到 RelativeCache添加装饰。
至此,配置和编码完成。
开始验证:
查询 userId=1的用户信息
{ "code":"1", "message":null, "data":{ "id":"1", "username":"admin", "password":"admin", "orgName":"组织1" } }
更新组织信息,将 组织1 改为 组织2
{ "code":"1", "message":null, "data":{ "id":"1", "name":"组织2" } }
再次查询用户信息
{ "code":"1", "message":null, "data":{ "id":"1", "username":"admin", "password":"admin", "orgName":"组织2" } }
符合预期。