【255期】面试官问:MyBatis 二级缓存,如何实现关联刷新功能?

点击上方“Java精选”,选择“设为星标”

别问别人为什么,多问自己凭什么!

下方有惊喜,留言必回,有问必答!

每一天进步一点点,是成功的开始...

1、MyBatis缓存介绍

Mybatis提供对缓存的支持,但是在没有配置的默认情况下,它只开启一级缓存,二级缓存需要手动开启。

一级缓存只是相对于同一个SqlSession而言。 也就是针对于同一事务,多次执行同一Mapper的相同查询方法,第一查询后,MyBatis会将查询结果放入缓存,在中间不涉及相应Mapper的数据更新(Insert,Update和Delete)操作的情况下,后续的查询将会从缓存中获取,而不会查询数据库。

二级缓存是针对于应用级别的缓存,也就是针对不同的SqlSession做到缓存。当开启二级缓存时,MyBatis会将首次查询结果存入对于Mapper的全局缓存,如果中间不执行该Mapper的数据更新操作,那么后续的相同查询都将会从缓存中获取。

2、二级缓存问题

根据二级缓存的介绍发现,如果Mapper只是单表查询,并不会出现问题,但是如果Mapper涉及的查询出现 联表 查询,如 UserMapper 在查询 user 信息时需要关联查询 组织信息,也就是需要 user 表和 organization 表关联,OrganizationMapper 在执行更新时并不会更新 UserMapper 的缓存,结果会导致在使用相同条件 使用 UserMapper 查询 user 信息时,会等到未更新前的 organization 信息,造成数据不一致的情况。

推荐下自己做的 Spring boot 的实战项目:
https://gitee.com/yoodb/jing-xuan‍

2.1、数据不一致问题验证

查询SQL

SELECT
 u.*, o.name org_name 
FROM
 user u
 LEFT JOIN organization o ON u.org_id = o.id 
WHERE
 u.id = #{userId}

UserMapper

UserInfo queryUserInfo(@Param("userId") String userId);

UserService

public UserEntity queryUser(String userId) {

    UserInfo userInfo = userMapper.queryUserInfo(userId);

    return userInfo;
}

调用查询,得到查询结果(多次查询,得到缓存数据),这里 userId = 1,data为user查询结果

{
 "code": "1",
 "message": null,
 "data": {
   "id": "1",
   "username": "admin",
   "password": "admin",
   "orgName": "组织1"
 }
}

查询 对应 organization 信息,结果

{
 "code": "1",
 "message": null,
 "data": {
   "id": "1",
   "name": "组织1"
 }
}

发现和user缓存数据一致。

推荐下几个月熬夜整理的近 10000+ 面试资料大全:https://gitee.com/yoodb/eboo‍ks

执行更新 organization 操作,将 组织1 改为 组织2,再次查询组织信息

{
 "code": "1",
 "message": null,
 "data": {
   "id": "1",
   "name": "组织2"
 }
}

再次查询user信息,发现依旧从缓存中获取

{
 "code": "1",
 "message": null,
 "data": {
   "id": "1",
   "username": "admin",
   "password": "admin",
   "orgName": "组织1"
 }
}

造成此问题原因为 organization 数据信息更新只会自己Mapper对应的缓存数据,而不会通知到关联表organization 的一些Mapper更新对应的缓存数据。

2.2、问题处理思路

  • 在 Mapper1 定义时,手动配置 相应的关联 Mapper2

  • 在 Mapper1 缓存 cache1 实例化时,读取 所关联的 Mapper2 的缓存 cache2相关信息

  • 在 cache1 中存储 cache2 的引用信息

  • cache1 执行clear时,同步操作 cache2 执行clear

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中配置<cache-ref namespace=“com.mars.system.dao.UserMapper”/>,不然查询结果不会被缓存化。如果接口为 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添加装饰。另外,更多面试题资料,公众号Java精选,回复java面试,获取最新面试资料,支持在线随时随地刷题。

至此,配置和编码完成。

开始验证:

查询 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"
    }
}

符合预期。

版权声明:本文为CSDN博主「方片龙」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

https://blog.csdn.net/qq_38245668/article/details/105803298

公众号“Java精选”所发表内容注明来源的,版权归原出处所有(无法查证版权的或者未注明出处的均来自网络,系转载,转载的目的在于传递更多信息,版权属于原作者。如有侵权,请联系,笔者会第一时间删除处理!

------ THE END ------

ea338a6584014ee74fd1f84573bdbe20.png精品资料,超赞福利!5afe4125a4a3e5382f7ff7b2dec4c3ee.png

>Java精选面试题<
3000+ 道面试题在线刷,最新、最全 Java 面试题!

bb231e7e95be2d076c45325a3603c5ad.png

087e8647c0dd2c758b5896fbc3b1e9f6.png

期往精选  点击标题可跳转

【247期】自从用了 OkHttp3,别的都完全不想用了!

【248期】如何在 Spring Boot 中使用异步方法优化 Service 逻辑提高接口响应速度?

【249期】Mybatis 源码解读 — 9 种设计模式,真是太有用了!

【250期】面试官问:谈一谈 MQ 消息幂等(去重)通用解决方案?

【251期】分享一款基于 SpringBoot 和 ElementUi 的 HC 小区物联网平台,附源码!

【252期】爆赞,对 volatile 关键字讲解最好的一篇文章!

【253期】京东二面:商品库存的扣除过程中,如何防止超卖问题?

【254期】这些 SQL 语句真是让我干瞪眼!

28bbf260f807e73dd1145a78a51213c6.png 技术交流群!649b4720c4a3f3e28aa6448452176309.png

最近有很多人问,有没有读者交流群!想知道如何加入?方式很简单,兴趣相投的朋友,只需要点击下方卡片,回复“加群”,即可无套路入交流群!

文章有帮助的话,在看,转发吧!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值