您的位置:首页 > 编程语言 > Java开发

Springboot整合集成ehcache3

2017-12-13 15:10 381 查看
1.pom.xml中添加依赖。spring-boot-starer-cache中已经有了ehcache和cache-api,可以不用再添加。

<!-- 本地缓存依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
-->

2. application.properties文件中添加配置位置。指定使用的cacheManger是jcache。
#-------------------ehcache----------------------
spring.cache.jcache.config=classpath:/config/ehcache.xml

3. 在配置的目录中新建添加ecache.xml文件。更详细的可以参照官方文档
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.ehcache.org/v3"
xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.2.xsd http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.2.xsd">
<persistence directory="java.io.tmpdir"/>

<cache-template name="default">
<resources>
<heap unit="entries">2000</heap>
<offheap unit="MB">100</offheap>
</resources>
</cache-template>

<cache alias="SSAccountInfo" uses-template="default">
<expiry>
<ttl unit="seconds">600</ttl>
</expiry>
</cache>

</config>

3.在配置类或者启动类上面,添加启用缓存的注解
@EnableCaching

4 . 在需要缓存的数据方法上面打上缓存注解。几个常用的缓存注解可参照官网。
package com.caiwufei.module.cfsss.dao;

import org.springframework.cache.annotation.Cacheable;
import com.caiwufei.entity.base.BaseMapper;
import com.caiwufei.entity.cfsss.SSAccountInfo;

public interface SSAccountInfoDao extends BaseMapper<SSAccountInfo>{

@Cacheable(cacheNames="SSAccountInfo", unless="#result==null")
public SSAccountInfo queryAccountInfoById(SSAccountInfo a);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: