Mybatis的二级缓存 (ehcache方式)
创始人
2024-04-10 07:59:57

目录

  • 前置
  • pom: jar
  • 配置文件: ehcache.xml
  • 配置指定方式
  • 恢复 .index 文件 (ApplicationClosedEventListener.java)
  • 效果图


前置

会演示二级缓存生效/失效的场景
项目地址: https://gitee.com/xmaxm/test-code/blob/master/chaim-cache/chaim-mybatis-cache/chaim-mybatis-cache-two/README.md

前置配置:

本篇文章是基于上篇文章进行: https://blog.csdn.net/qq_38637558/article/details/127924334

注意点

官网地址:
https://www.ehcache.org/documentation
http://mybatis.org/ehcache-cache/index.html
如清除策略、可读或可读写等,不能应用于自定义缓存

强调:

通过.index文件进行数据的恢复. 可参考 ApplicationClosedEventListener.java
磁盘已经写入了数据, 重启项目的时候, 发现还是从数据库查, 第一次命中率还是0, 需要执行以下方法.

源码部分

感觉要是把源码过一遍, 得从新起一篇文章才行, 后面有需要在写, 偷个懒吧, 哈哈哈哈哈!

源码入口: org.apache.ibatis.mapping.CacheBuilder#build
关键类: org.apache.ibatis.cache.Cache
实现类: org.mybatis.caches.ehcache.EhcacheCache
org.mybatis.caches.ehcache.AbstractEhcacheCache
淘汰策略: 没得

相关缓存文章

Mybatis的一级缓存
Mybatis的二级缓存 (默认方式)
Mybatis的二级缓存 (Redis方式)
Mybatis的二级缓存 (ehcache方式)


pom: jar

没有使用过这个cache框架, 不过整体实现代码也没有几个类, 可以读一下


org.mybatis.cachesmybatis-ehcache1.2.2true


配置文件: ehcache.xml




配置指定方式

配置:
@CacheNamespace(implementation = EhcacheCache.class)

备注:
@CacheNamespace(implementation = EhcacheCache.class)
对应的xml文件:

---------------------------------------
@CacheNamespaceRef(value = SysUserMapper.class)
对应的xml文件:

@CacheNamespace(implementation = EhcacheCache.class)
public interface SysUserMapper extends BaseMapper {
}


恢复 .index 文件 (ApplicationClosedEventListener.java)

package com.chaim.mybatis.cache.two.config;import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;/*** 监听项目停止运行* 用于处理 mybatis-ehcache 停止时调用 shutdown() 方法, 生成用于恢复的 .index 文件* 当 CacheManager 存在的时候该类才会进行 IOC** @author Chaim* @date 2022/9/10 2:14*/
@ConditionalOnClass(name = {"net.sf.ehcache.CacheManager"})
@Component
@Slf4j
public class ApplicationClosedEventListener implements ApplicationListener {/*** 这一串最终执行的代码:* CacheManager cacheManager = CacheManager.getInstance();* cacheManager.shutdown();* 考虑到可能自定义二级缓存不采用此方式, 那么pom就不会导入对应的jar, 避免代码报错使用反射方式** @param contextClosedEvent*/@SneakyThrows@Overridepublic void onApplicationEvent(ContextClosedEvent contextClosedEvent) {// 通过反射避免引入包 CacheManager 不存在编译报错Class aClass = Class.forName("net.sf.ehcache.CacheManager");// 静态方法Method getInstance = aClass.getMethod("getInstance");Object invoke = getInstance.invoke(aClass);Method shutdown = aClass.getMethod("shutdown");shutdown.invoke(invoke);log.info("程序已停止");}
}

效果图

在这里插入图片描述
在这里插入图片描述

上一篇:Java中的线程

下一篇:矩阵论复习提纲

相关内容

热门资讯

三未信安科技股份有限公司202... 证券代码:688489 证券简称:三未信安 公告编号:2026-002三未信安科技股份有限公司20...
浙江康隆达特种防护科技股份有限... 证券代码:603665 证券简称:康隆达 公告编号:2026-006浙江康隆达特种防护科技股份有限公...
山东鲁北化工股份有限公司202... 股票代码:600727 股票简称:鲁北化工 编号:2026-002山东鲁北化工股份有限公司2025...
四川西昌电力股份有限公司关于控... 证券代码:600505 证券简称:西昌电力 公告编号:2026-005四川西昌电力股份有限公司关于...
首药控股(北京)股份有限公司2... 证券代码:688197 证券简称:首药控股 公告编号:2026-001首药控股(北京)股份有限公司...