1.导入依赖
<!-- springboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- EhCache 缓存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
2.创建 ehcache.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- 磁盘路径 -->
<diskStore path="java.io.tmpdir"/>
<!-- 默认缓存 -->
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="1800" timeToLiveSeconds="0" overflowToDisk="true"/>
<!-- 登录缓存,闲置 30 分钟失效 -->
<cache name="loginCache" maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="1800" timeToLiveSeconds="0" overflowToDisk="true"/>
</ehcache>
磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于 Windows 系统的虚拟内存
- path:指定在硬盘上存储对象的路径
- path可以配置的目录有:
- user.home(用户根目录)
- user.dir(用户当前工作目录)
- java.io.tmpdir(默认临时目录)
- ehcache.disk.store.dir(ehcache的配置目录)
绝对路径(如:c:\cache)
程序中查看磁盘路径:String tmpDir = System.getProperty(“java.io.tmpdir”);
缓存配置:
- name: 缓存名称;
- maxElementsInMemory: 缓存对象的最大个数;
- eternal: 缓存对象是否永久有效;
- timeToIdleSeconds: 缓存对象闲置时间(单位:秒,仅当eternal=false生效,可选属性),当两次访问时间超过设定时间时缓存对象失效(默认 0,表示闲置时间无穷大);
- timeToLiveSeconds: 缓存对象存活时间(单位:秒,仅当eternal=false生效,可选属性),到达设定时间时缓存对象失效(默认 0,表示无穷生命);
- overflowToDisk: 当内存中的缓存对象数量超过 maxElementsInMemory 设定值时,将溢出的缓存对象存到磁盘中;
- diskSpoolBufferSizeMB: 缓存占用磁盘的缓存区大小(默认 30MB,每个Cache的缓存区独立存在);
- maxElementsOnDisk: 硬盘缓存对象最大个数;
- diskPersistent: 是否缓存虚拟机重启期数据(默认 false);
- diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔(默认 120秒);
- memoryStoreEvictionPolicy: 缓存对象个数超出 maxElementsInMemory 设定值时,根据指定的策略去清理内存。默认策略 LRU(最近最少使用)其他策略 FIFO(先进先出) LFU(较少使用);
- clearOnFlush: 内存数量最大时是否清除;
3.application.properties 配置
# 缓存类型
spring.cache.type=ehcache
# 缓存 ehcache 配置文件路径
spring.cache.ehcache.config=classpath:ehcache.xml
4.启动类注解启用缓存
@EnableCaching
@SpringBootApplication
public class EngineApp {
public static void main(String[] args) {
SpringApplication.run(EngineApp.class, args);
}
}
5.创建缓存操作工具类
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.util.ObjectUtils;
/**
* @author echoo华地
* @Description EhCache 缓存工具类
* @date 2022/5/27 15:17
**/
@Slf4j
public final class EhCacheUtils {
/**
* 默认缓存名
*/
public static final String CACHE_NAME = "loginCache";
/**
* 缓存管理器
*/
private static final CacheManager CACHE_MANAGER = SpringBootUtils.getBean(CacheManager.class);
/**
* @return org.springframework.cache.Cache
* @description 获取默认缓存对象
* @author echoo华地
* @date 2022/5/27 15:44
*/
public static Cache getCache() {
return CACHE_MANAGER.getCache(EhCacheUtils.CACHE_NAME);
}
/**
* @param key
* @param value
* @return void
* @description 添加缓存数据
* @author echoo华地
* @date 2022/5/27 15:45
*/
public static void put(String key, Object value) {
try {
Cache cache = getCache();
cache.put(key, value);
} catch (Exception e) {
log.error("添加缓存失败;错误:[{}]", e.getMessage());
}
}
/**
* @param key
* @return T
* @description 获取缓存数据
* @author echoo华地
* @date 2022/5/27 15:46
*/
public static <T> T get(String key) {
try {
Cache cache = getCache();
Cache.ValueWrapper valueWrapper = cache.get(key);
if (ObjectUtils.isEmpty(valueWrapper)) {
return null;
}
return (T) valueWrapper.get();
} catch (Exception e) {
log.error("获取缓存数据失败;错误:[{}]", e.getMessage());
return null;
}
}
/**
* @param key
* @return void
* @description 删除缓存数据
* @author echoo华地
* @date 2022/5/27 15:47
*/
public static void delete(String key) {
try {
Cache cache = getCache();
cache.evict(key);
} catch (Exception e) {
log.error("删除缓存数据失败:", e);
}
}
}