springboot配置缓存Ehcache及使用

一、在创建springboot的时候勾选cache

二、在pom.xml中添加依赖

<!-- Spring Boot 缓存支持启动器 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.10.4</version>
</dependency>

三、在application.properties中进行配置

Spring.cache.type=ehcache
Spring.cache.ehcache.config=classpath:config/ehcache.xml

ehcache.xml中的内容

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
    <cache name="jyy" 
    eternal="true"
    maxentriesLocalHeap="0"
    timeToLiveSeconds="300"/>
<!--     name为缓存的名字 -->
<!-- eternal表示对象永不过期,false表示会过期, timeToLiveSeconds表示过久之后过期 -->
<!-- maxentriesLocalHeap表示堆内存中最大的缓存对象数,0表示没有限制 -->
<!-- timeToLiveSeconds,以秒为单位,当对象自最近一次访问后,如果处于空闲状态超过timeToLiveSeconds后,则缓存失效 -->
</ehcache>

四、在启动类前添加Ehcache注解

五、使用时只需在业务层中类的方法上添加注解即可

    

在业务层方法上,写上你配置的缓存的name即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值