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、 代碼下載下傳
這裡,大家可以自己下載下傳下來跑一下,體驗一下