在springboot中配置ehcache3蒋情,并開啟Cache監(jiān)聽
一、在項(xiàng)目中啟用ehcache3
1耸携、在pom.xml中添加依賴
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.9.9</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.1</version>
</dependency>
添加starter-cache
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2棵癣、添加配置文件ehcache3.xml
創(chuàng)建配置文件src\main\resources\ehcache3.xml
,配置文件文件名可以改夺衍,此處命名為ehcache3.xml
狈谊,并修改application.properties
文件,加入緩存配置
spring.cache.jcache.config=classpath:/ehcache3.xml
3沟沙、完成配置文件配置項(xiàng)
修改ehcache3.xml
文件內(nèi)容河劝,參考如下
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns='http://www.ehcache.org/v3'>
<cache alias="foo">
<key-type>java.lang.String</key-type>
<resources>
<heap unit="entries">2000</heap>
<offheap unit="MB">500</offheap>
</resources>
</cache>
<cache-template name="myDefaults">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<heap unit="entries">200</heap>
</cache-template>
<cache alias="bar" uses-template="myDefaults">
<key-type>java.lang.Number</key-type>
</cache>
<cache alias="simpleCache" uses-template="myDefaults" />
<cache alias="tasks">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<expiry>
<ttl unit="minutes">1</ttl>
</expiry>
<resources>
<heap unit="kB">10</heap>
</resources>
</cache>
</config>
ehcache3采用了xml格式配置文件,可配置模板矛紫,可以為不同的key和value設(shè)置不同的cache赎瞎,詳情可參考官網(wǎng)配置
4、在項(xiàng)目中啟用緩存
在啟用的文件中加入@EnableCaching
@EnableCaching
@SpringBootApplication
public class Cache3Application {
public static void main(String[] args) {
SpringApplication.run(Cache3Application.class, args);
}
}
二颊咬、在業(yè)務(wù)邏輯中使用緩存
可在業(yè)務(wù)邏輯中使用
- @Cacheable
- @CachePut
- @CacheEvict
等對緩存進(jìn)行操作务甥,如下代碼所示
@GetMapping("/s/{key}")
@Cacheable(value="tasks",key="'s_' + #key")
public String search(@PathVariable String key){
return "STR:" + key;
}
@GetMapping("/find")
@Cacheable(value="tasks",key="'findall_key'") // Add this
public String findAll() {
log.info("findAll tasks");
return "list";
}
@GetMapping("/put")
@CachePut(value="tasks",key="'findall_key'")
public String put() {
log.info("put to cache");
return "put()";
}
@GetMapping("/del")
@CacheEvict(value="tasks",key="'findall_key'")
public String delCache(){
log.info("delCache...");
return "delcache";
}
其中value為配置的緩存名稱,key可以是一個字符串喳篇,也可以是參數(shù)變量敞临,須放在key內(nèi)部,注意其中引號的使用
三麸澜、配置緩存監(jiān)聽
1挺尿、實(shí)現(xiàn)一個監(jiān)聽類
在項(xiàng)目中添加一個監(jiān)聽器類,描述當(dāng)事件發(fā)生時的處理炊邦,此處只是通過日志打印顯示编矾,代碼如下
public class MyCacheListener implements CacheEventListener<String,String> {
private static final Logger log = LoggerFactory.getLogger(MyCacheListener.class);
public MyCacheListener() {
log.info("MyCacheListener: init");
}
@Override
public void onEvent(CacheEvent<? extends String, ? extends String> cacheEvent) {
log.info("'{}' : [{}] --> {}", cacheEvent.getType(), cacheEvent.getKey(), cacheEvent.getNewValue());
}
}
2、配置監(jiān)聽
在ehcache3.xml
中配置對緩存的監(jiān)聽铣耘,添加<listeners>
部分洽沟,結(jié)果如下
<cache alias="tasks">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<expiry>
<ttl unit="minutes">1</ttl>
</expiry>
<listeners>
<listener>
<class>com.zhouf.cache3.listener.MyCacheListener</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>ORDERED</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>
</listener>
</listeners>
<resources>
<heap unit="kB">10</heap>
</resources>
</cache>
其中描述了監(jiān)聽的配置以故,對哪些事件需要作響應(yīng)蜗细,可參考官網(wǎng)文檔