一、目录
- 简介
- 整合SpringBoot
- EhCacheUtils编写
二、简介
2.1 基本介绍
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认CacheProvider。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现。它支持注解方式使用缓存,非常方便。
2.2 主要的特性
- 快速
- 简单
- 多种缓存策略
- 缓存数据有两级:内存和磁盘,因此无需担心容量问题
- 缓存数据会在虚拟机重启的过程中写入磁盘
- 可以通过RMI、可插入API等方式进行分布式缓存
- 具有缓存和缓存管理器的侦听接口
- 支持多缓存管理器实例,以及一个实例的多个缓存区域
- 提供Hibernate的缓存实现
2.3 集成
可以单独使用,一般在第三方库中被用到的比较多(如mybatis、shiro等)ehcache 对分布式支持不够好,多个节点不能同步,通常和redis一块使用
2.4 ehcache 和 redis 比较
ehcache直接在jvm虚拟机中缓存,速度快,效率高;但是缓存共享麻烦,集群分布式应用不方便。
redis是通过socket访问到缓存服务,效率比Ehcache低,比数据库要快很多,处理集群和分布式缓存方便,有成熟的方案。如果是单个应用或者对缓存访问要求很高的应用,用ehcache。如果是大型系统,存在缓存共享、分布式部署、缓存内容很大的,建议用redis。
ehcache也有缓存共享方案,不过是通过RMI或者Jgroup多播方式进行广播缓存通知更新,缓存共享复杂,维护不方便;简单的共享可以,但是涉及到缓存恢复,大数据缓存,则不合适。
三、整合SpringBoot
3.1 导入maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
3.2 编写ehcache.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!--
磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
path:指定在硬盘上存储对象的路径
path可以配置的目录有:
user.home(用户的家目录)
user.dir(用户当前的工作目录)
java.io.tmpdir(默认的临时目录)
ehcache.disk.store.dir(ehcache的配置目录)
绝对路径(如:d:\\ehcache)
查看路径方法:String tmpDir = System.getProperty("java.io.tmpdir");
-->
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="true" />
<cache name="myCache" maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="3600" overflowToDisk="true" />
<!--
缓存配置
name: 缓存名称。
maxElementsInMemory: 缓存最大个数。
eternal: 对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds: 设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds: 设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk: 当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB: 这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk: 硬盘最大缓存个数。
diskPersistent: 是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy: 当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush: 内存数量最大时是否清除。
-->
</ehcache>
3.3 添加springboot 配置(application.yml)
spring:
cache:
type: ehcache
ehcache:
config: classpath:ehcache.xml
3.4 spring boot启动类开启缓存注解
@EnableCaching
四、编写EhCacheUtils
/*
* Copyright © 2020-present hkt-star. All Rights Reserved.
*/
package com.wanwei.onevieweecf.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;
/**
* ehcache缓存操作工具类
*
* @author hkt
* @version 1.0
* @title EhCacheUtils
* @date 2021/3/1 11:56
*/
@Slf4j
public final class EhCacheUtils {
/** CacheManager */
private static final CacheManager CACHE_MANAGER = SpringUtils.getBean(CacheManager.class);
/**
* 获取Cache
*
* @author hkt
* @date 2021/3/1 12:49
*/
public static Cache getCache() {
return CACHE_MANAGER.getCache("myCache");
}
/**
* 添加缓存数据
*
* @param key 键
* @param value 值
* @author hkt
* @date 2021/3/1 12:50
*/
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 缓存数据
* @author hkt
* @date 2021/3/1 12:53
*/
public static <T> T get(String key) {
try {
Cache cache = getCache();
return (T) cache.get(key).get();
} catch (Exception e) {
log.error("获取缓存数据失败:", e);
return null;
}
}
/**
* 删除缓存数据
*
* @param key 键
* @author hkt
* @date 2021/3/1 12:53
*/
public static void delete(String key) {
try {
Cache cache = getCache();
cache.evict(key);
} catch (Exception e) {
log.error("删除缓存数据失败:", e);
}
}
/**
* @author hkt
* @date 2021/3/1 12:02
*/
private EhCacheUtils() {
}
}
版权声明:本文为qq_33501272原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。