1、pom.xml中引入相关依赖包
<!-- 缓存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
2、启动类加入如下注解(不加这个注解,CacheManager会报缺少bean的注入异常)
@EnableCaching
3、resources目录下建ehcache.xml文件,模板如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ehcache https://blog.csdn.net/vbirdbest/article/details/72763048 -->
<ehcache updateCheck="false" name="glchseatbelt">
<!-- 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->
<!--<diskStore path="java.io.tmpdir/ehcache"/>-->
<!--
user.home 用户的主目录
user.dir 用户的当前工作目录
java.io.tmpdir 默认的临时文件路径
-->
<diskStore path="user.dir/cache"/>
<!-- name 缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)。 -->
<!-- maxEntriesLocalHeap 磁盘缓存中最多可以存放的元素数量,0表示无穷大。 -->
<!-- eternal 缓存中对象是否永久有效,即是否永驻内存,true时将忽略timeToIdleSeconds和timeToLiveSeconds -->
<!-- timeToIdleSeconds 缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,
此为可选属性即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除。 -->
<!-- timeToLiveSeconds 缓存数据在失效前的允许存活时间(单位:秒),仅当eternal=false时使用,默认值是0表示可存活时间无穷大,
即Cache中的某元素从创建到清楚的生存时间,也就是说从创建开始计时,当超过这个时间时,此元素将从Cache中清除。 -->
<!-- overflowToDisk 内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中),
会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件的名称是cache的名称,后缀名是data。 -->
<!--diskExpiryThreadIntervalSeconds:磁盘缓存的清理线程运行间隔-->
<!--memoryStoreEvictionPolicy:缓存清空策略-->
<!--1.FIFO:first in first out 先进先出-->
<!--2.LFU: Less Frequently Used 一直以来最少被使用的-->
<!--3.LRU:Least Recently Used 最近最少使用的-->
<!-- 默认缓存 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- 对应的时间总共的识别的数据量 -->
<cache name="recogCount"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
diskPersistent = "true"
overflowToDisk="false"
statistics="true">
</cache>
<cache
name="TestCache"
maxElementsInMemory="1"
eternal="true"
overflowToDisk="true"
maxElementsOnDisk="0"
diskPersistent="true"/>
</ehcache>
4、配置类中加载ehcache.xml文件
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@Configuration
@ConditionalOnProperty(name={"enabled"}, havingValue="true", prefix="ehcache")
public class EhCacheConfig
{
public static final String EH_CACHE_CLASS_PATH = "config/ehcache/ehcache.xml";
@Bean(name={"appEhCacheCacheManager"})
public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean)
{
return new EhCacheCacheManager(bean.getObject());
}
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean()
{
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("config/ehcache/ehcache.xml"));
cacheManagerFactoryBean.setShared(true);
return cacheManagerFactoryBean;
}
}
5、注入CacheManager
@Autowired
CacheManager cacheManager;
6、使用Cache
// 存入Cache的例子
String key = UUID.randomUUID();
// 其中RESULTCACHENAME为ehcache.xml中Cache的name值
Cache cache = this.cacheManager.getCache(RESULTCACHENAME);
Object obj = new Object();
cache.put(key, obj);
//***************************************
// 从Cache取出的例子
Cache cache = this.cacheManager.getCache(RESULTCACHENAME);
Student stu =(Student )cache.get(key, Student .class);