天天看点

Spring Boot整合ehcache3.0 缓存Spring Boot整合ehcache3.0以上缓存

Spring Boot整合ehcache3.0以上缓存

1、题记

  • 目前市面上大多数文章都是ehcache2.0的版本,由于ehcache3.0改动较大,原来的配置方式已经不使用了,故小小研究了一番,笔以记之。

2、 先配置Spring boot

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion></modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>ehcache3-jsr107-spring-boot</artifactId>
    <version>-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>ehcache3-jsr107-spring-boot</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId> <!--parent project to use spring boot -->
        <version>.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
        <maven.compiler.source></maven.compiler.source>
        <maven.compiler.target></maven.compiler.target>
    </properties>


    <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-cache</artifactId> <!--Starter for using Spring Framework's caching support-->
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId> <!-- starter for using Spring MVC -->
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <jvmArguments>-Xmx64m -XX:MaxDirectMemorySize=512m</jvmArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
           

添加我们今天的主角:

<dependency>
          <groupId>javax.cache</groupId> <!-- JSR-107 API-->
          <artifactId>cache-api</artifactId>
        </dependency>
        <dependency>
          <groupId>org.ehcache</groupId>
          <artifactId>ehcache</artifactId>
          <version>3.4.0</version>
        </dependency>
           

cache-api也必须要引入,因为目前ehcache遵循jsr-107规范,所以必须引入

包就引入完毕了,看配置文件

3、ehcache配置文件

<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>
      <jsr107:cache name="people" template="heap-cache"/>
    </jsr107:defaults>
  </service>

  <cache-template name="heap-cache">
    <listeners>
        <listener>
          <class>org.vrzart.com.ehcache.EventLogger</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>
      <heap unit="entries">2000</heap>
      <offheap unit="MB">100</offheap> <!-- unit of measure is case sensitive! -->
    </resources>
  </cache-template>

  <cache alias="peopleCache" uses-template="heap-cache">
    <expiry>
      <ttl unit="seconds">600</ttl>
    </expiry>
  </cache>

</config>
           

配置spring 配置文件 application.properties

spring.cache.jcache.config=classpath:ehcache.xml  ###attention,这里是jcache
server.port=8090
           

4、使用代码

@Component
@CacheDefaults(cacheName = "peopleCache")
public class PersonService
{
    private static final Logger LOGGER = LoggerFactory.getLogger(PersonService.class);

    @CacheResult
    public Person getPerson(int ssn)
    {
        LOGGER.info("ssn " + ssn + " not found in cache. TimeStamp: {}", new Date());

        switch (ssn)
        {
            case :
                return new Person(ssn, "Geoff", "Gibson");
            case :
                return new Person(ssn, "Cory", "Beck");
            default:
                return new Person(ssn,"John","Doe");
        }
    }


}
           

使用的jsr-107标准注解,没有使用Spring的一些注解(如cachable等)

5、测试

启动SpringJsr107Ehcache3Application类,浏览器输入[这个地址

]( http://localhost:8090/person/987654321)

多刷新自己就会发现从缓存中拿数据了

6、 代码下载

这里,大家可以自己下载下来跑一下,体验一下