一、在创建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即可。