背景
最近把本站的JDK版本从11升级到了17,springboot也升级到了3。之前coderbbb使用的是EhCache2+Springboot2的方案,所以需要升级到EhCache3+Springboot3。
首先,要明确一点,就是EhCache2和EhCache3可以说是完全不同的两个框架了。EhCache2有自己的一套缓存标准API,springboot2的cache部分整合了该API。但是到了EhCache3,并没有自己的API,而是实现了JCache。springboot3中,也不再有EhCacheCacheManager
这个可以自动注入的类了。
综上所述,EhCache3和Springboot3的搭配使用,和之前有很大区别。本文将详细介绍具体整合步骤。
整合
下载依赖包
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.10.8</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
第一个依赖包是EhCache3的依赖包,后面的几个主要是用于解析EhCache3的xml配置文件的。具体每个依赖的最新版本和作用,后面作者再更新。
appliaction.properties配置
第二步需要在springboot的application.properties中配置springboot cache选用哪种缓存实现(比如redis、jcache等等),以及配置文件位置。
spring.cache.type=jcache
spring.cache.jcache.config=classpath:ehcache3.xml #这个文件名可以自己定义
这里需要注意的是,缓存类型不再是EhCache了,EhCache3是实现JCache的一种缓存。
Ehcache3的xml文件配置
该配置文件比较简单,虽然和Ehcache2的配置文件完全不同,但参考代码中的注释,应该可以看懂。
<config xmlns='http://www.ehcache.org/v3'>
<!-- 持久化 路径 -->
<persistence directory="./tmp/Ehcache3"/>
<!-- 缓存模版,此处为了显示其用法,也可以不用模版直接在cache中配置与模版参数相同 -->
<cache-template name="oneHourTemplate">
<!-- <key-type>java.lang.String</key-type>-->
<!-- <value-type>java.lang.String</value-type>-->
<expiry>
<!-- 单位默认为秒当用秒作单位时,可以不填-->
<ttl unit="hours">1</ttl>
</expiry>
<resources>
<!-- 单位默认为entries当用entries作单位时,可以不填-->
<heap>100</heap>
<offheap unit="MB">5</offheap>
<!-- persistent 默认为false可以不填-->
<disk unit="MB">1024</disk>
</resources>
</cache-template>
<cache-template name="tenMinutesTemplate">
<!-- <key-type>java.lang.String</key-type>-->
<!-- <value-type>java.lang.String</value-type>-->
<expiry>
<!-- 单位默认为秒当用秒作单位时,可以不填-->
<ttl unit="minutes">10</ttl>
</expiry>
<resources>
<!-- 单位默认为entries当用entries作单位时,可以不填-->
<heap>100</heap>
<offheap unit="MB">5</offheap>
<!-- persistent 默认为false可以不填-->
<disk unit="MB">1024</disk>
</resources>
</cache-template>
<!-- 缓存对象,如果使用了模版会覆盖模版中的内容,使用uses-template=""来引用模版 -->
<cache alias="indexArticleList" uses-template="oneHourTemplate">
<!-- <key-type>java.lang.Integer</key-type>-->
<!-- <value-type>com.github.pagehelper.PageInfo</value-type>-->
<!-- <expiry>-->
<!-- <!–此处会覆盖模版中的(TTL)配置 –>-->
<!-- <tti>60</tti>-->
<!-- </expiry>-->
<!-- <resources>-->
<!-- <disk unit="MB" persistent="true"> 500</disk>-->
<!-- </resources>-->
<!-- 没有研究这块,暂时先不管
<eviction-advisor></eviction-advisor>
-->
</cache>
<cache alias="articleDetailsBig" uses-template="oneHourTemplate">
<!-- <key-type>java.lang.Long</key-type>-->
<!-- <value-type>com.coderbbb.blogv2.database.dto.WebsiteArticleDTO</value-type>-->
</cache>
<cache alias="cacheHtml" uses-template="oneHourTemplate" />
<cache alias="articleDo" uses-template="tenMinutesTemplate">
<!-- <key-type>java.lang.Long</key-type>-->
<!-- <value-type>com.coderbbb.blogv2.database.dos.ArticleDO</value-type>-->
</cache>
<cache alias="articleKeywords" uses-template="oneHourTemplate">
<!-- <key-type>java.lang.Long</key-type>-->
<!-- <value-type>java.lang.String</value-type>-->
</cache>
<cache alias="websiteConfig" uses-template="oneHourTemplate">
<!-- <key-type>java.lang.String</key-type>-->
<!-- <value-type>com.coderbbb.blogv2.database.dos.WebsiteConfigDO</value-type>-->
</cache>
<cache alias="randomArticle" uses-template="oneHourTemplate">
<!-- <key-type>java.lang.Long</key-type>-->
<!-- <value-type>java.util.List</value-type>-->
</cache>
<cache alias="cacheUserAgent" uses-template="tenMinutesTemplate">
<!-- <key-type>java.lang.String</key-type>-->
<!-- <value-type>java.lang.Boolean</value-type>-->
</cache>
<cache alias="authorInfo" uses-template="oneHourTemplate" />
<cache alias="relationArticleInfo" uses-template="oneHourTemplate" />
<cache alias="userAnaData" uses-template="tenMinutesTemplate" />
<cache alias="userCache" uses-template="tenMinutesTemplate" />
<cache alias="indexTag" uses-template="oneHourTemplate" />
<cache alias="hotArticle" uses-template="oneHourTemplate" />
<cache alias="recentArticle" uses-template="oneHourTemplate" />
<cache alias="hotBook" uses-template="oneHourTemplate" />
</config>
开始在springboot3中使用EhCache3
这一步和之前是一样的,通过springboot中的@Cacheable
等注解,正常使用缓存即可。
运行截图
整合成功后,EhCache3的日志输出是远远多于EhCache2的。如下图所示,已经可以正常使用EhCache3啦!