Springboot3 项目mybatis-plus配置ehcache缓存

370 阅读1分钟

背景

项目中一些不经常变更的数据,在查询的时候配置缓存,减少数据库访问,加快速度。

maven依赖

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-cache</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId> 
    <classifier>jakarta</classifier>
</dependency>

启动类开启缓存并编写ehcache配置文件

image.png

监听缓存key事件类


import org.ehcache.event.CacheEvent;
import org.ehcache.event.CacheEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomCacheEventLogger implements CacheEventListener<Object, Object> {

    private static final Logger LOG = LoggerFactory.getLogger(CustomCacheEventLogger.class);

    @Override
    public void onEvent(CacheEvent cacheEvent) {
        LOG.info("Cache event = {}, Key = {},  Old value = {}, New value = {}", cacheEvent.getType(),
                cacheEvent.getKey(), cacheEvent.getOldValue(), cacheEvent.getNewValue());
    }
}

项目配置文件application.xml或者bootstrap.xml新增配置:

spring.cache.jcache.config=classpath:ehcache.xml

ehcache配置文件(项目classpath路径下):

<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xmlns:jsr107='http://www.ehcache.org/v3/jsr107'>

    <service>
        <jsr107:defaults enable-statistics="true"/>
    </service>

    <cache alias="smsConfigFindAll">
        <key-type>org.springframework.cache.interceptor.SimpleKey</key-type>
        <value-type>java.util.ArrayList</value-type>
        <expiry>
            <ttl unit="seconds">10</ttl>
        </expiry>
        <listeners>
            <listener>
                <class>com.hpe.osp.ospmsisdnresourcedownload.config.CustomCacheEventLogger</class>
                <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                <event-ordering-mode>UNORDERED</event-ordering-mode>
                <events-to-fire-on>CREATED</events-to-fire-on>
                <events-to-fire-on>UPDATED</events-to-fire-on>
                <events-to-fire-on>EXPIRED</events-to-fire-on>
                <events-to-fire-on>REMOVED</events-to-fire-on>
                <events-to-fire-on>EVICTED</events-to-fire-on>
            </listener>
        </listeners>
        <resources>
            <offheap unit="MB">100</offheap>
        </resources>
    </cache>
    <cache alias="smsConfigFindById">
        <key-type>java.lang.Long</key-type>
        <value-type>com.hpe.osp.ospmsisdnresourcedownload.entity.SmsConfig</value-type>
        <expiry>
            <ttl unit="seconds">10</ttl>
        </expiry>
        <listeners>
            <listener>
                <class>com.hpe.osp.ospmsisdnresourcedownload.config.CustomCacheEventLogger</class>
                <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                <event-ordering-mode>UNORDERED</event-ordering-mode>
                <events-to-fire-on>CREATED</events-to-fire-on>
                <events-to-fire-on>UPDATED</events-to-fire-on>
                <events-to-fire-on>EXPIRED</events-to-fire-on>
                <events-to-fire-on>REMOVED</events-to-fire-on>
                <events-to-fire-on>EVICTED</events-to-fire-on>
            </listener>
        </listeners>
        <resources>
            <offheap unit="MB">100</offheap>
        </resources>
    </cache>

</config>

说明:

smsConfigFindAll查询缓存,请求无参数,目的查询list。

smsConfigFindById根据Id查询。

ehchche.xml配置文件中 <key-type> 缓存方法的key类型(String, Long, org.springframework.cache.interceptor.SimpleKey) <value-type>配置返回类型。 <expiry>配置缓存失效时间。<listeners> <listener>配置缓存事件的监听类。<resources> <offheap>配置缓存的大小(非堆内存)。

service类缓存用法

新增更新方法上添加注解@CacheEvict让缓存失效,注意缓存默认是根据方法的参数做key来进行缓存的,我们这里让这个"smsConfigFindAll"的缓存失效,添加allEntties=true

image.png

方法上添加缓存注解@Cacheable

image.png

bootstrap.yml或者application.yml打印sql(此处为mybatis-plus)

mybatis-plus:
  configuration:
    cache-enabled: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

访问service方法发现第一次查询打印sql,第二次没有,10秒过期之后再查询会继续打印sql。