SpringBoot 整合缓存 Ehcache 代码详解、ehcache.xml 配置详解

SpringBoot 整合缓存 Ehcache 代码详解、ehcache.xml 配置详解
- 前言:

Ehcache 和 SpringBoot 内置的缓存 Simple cache 缓存在编码使用上是没有什么区别的,只是要引入的依赖不同,以及相应的配置文件也不同。故,请先参照SpringBoot 使用内置缓存代码详解(数据从缓存中存、取代码讲解)@Cacheable、@CachePut注解讲解这篇博客进行SpringBoot 内置的缓存的初步了解后,再来了解 Ehcache。

- 引入相关依赖:
<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <optional>trueoptional>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-testartifactId>
    <scope>testscope>
dependency>


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-cacheartifactId>
dependency>
<dependency>
    <groupId>net.sf.ehcachegroupId>
    <artifactId>ehcacheartifactId>
dependency>

<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-boot-starterartifactId>
    <version>3.4.3version>
dependency>

<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.28version>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
- yml配置:
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
    username: user
    password: 123456
    
  # 配置使用Ehcache, SpringBoot 默认缓存是 cache: type: simple
  cache:
    type: ehcache

mybatis-plus:
  global-config:
    db-config:
      table-prefix: tbl_
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

- 使用Ehcache需要读取ehcache.xml中的配置:

SpringBoot 整合缓存 Ehcache 代码详解、ehcache.xml 配置详解_第1张图片

- 相关数据层代码、业务层代码以及表现层代码同SpringBoot 使用内置缓存代码详解(数据从缓存中存、取代码讲解)@Cacheable、@CachePut注解讲解这篇博客
- ehcache.xml配置:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">

	
    <diskStore path="D:\\aspring\\ehcache"/>

    

    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="false"
        maxElementsOnDisk="10000000"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU"
    />

    <cache
        name="cacheSpace"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="false"
        maxElementsOnDisk="10000000"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU"
    />

ehcache>
@Cacheable(value="cacheSpace", key="#id")
public Book getCacheById(String id) {
    return getById(id);
}
- 在使用 @Cacheable(value=“cacheSpace”, key=“#id”) 注解时:如果value省略,那么会使用 ehcache.xml 中的 中的配置。指明 value 后,同样要在 ehcache.xml 中有和 value 同名的配置。
- 使用Postman测试结果如下:
  • 先执行 GET 请求,将相关数据放到缓存中:
    SpringBoot 整合缓存 Ehcache 代码详解、ehcache.xml 配置详解_第2张图片

  • 在执行 POST 请求做校验:
    SpringBoot 整合缓存 Ehcache 代码详解、ehcache.xml 配置详解_第3张图片

你可能感兴趣的:(SpringBoot,spring,boot,缓存,ehcache)