Springboot与Ehcache3使用与整合
前言
springboot中ehcache3配置有两种方式,xml配置及Java配置,对于强迫症的我这里只介绍Java配置更符合springboot的结构。
一、pom.xml引入相关jar
<!-- Ehcache 缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
二、引入Java配置类
代码如下(示例):
import java.io.File;
import java.time.Duration;
import org.ehcache.CacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.impl.serialization.PlainJavaSerializer;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class Ehcache3Config {
private static final String CACHE_NAME_USER = "cache-user";
private static final String CACHE_NAME_SYS = "cache-sys";
@Bean
public CacheManager ehcacheManager() {
//配置占用资源
//heap 设置占用虚拟机内存大小
//offheap 设置占用虚拟机外内存大小
//disk 设置占用磁盘空间大小,需结合下方ehcacheManager设置存储位置,否则会报错
ResourcePoolsBuilder resourcePoolsBuilder =ResourcePoolsBuilder.newResourcePoolsBuilder().heap(20, MemoryUnit.MB).offheap(30, MemoryUnit.MB).disk(100, MemoryUnit.MB);
//配置存储键值格式及过期策略
//如:withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofHours(24)) 最后一次使用后计算,所存储的时间超过24小时
//如:withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofHours(24))) 从缓存开始计算,所存储的时间超过24小时
//如:withExpiry(ExpiryPolicyBuilder.noExpiration()) 永不过期
CacheConfiguration<String, Object> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, Object.class, resourcePoolsBuilder)
.withValueSerializer(new PlainJavaSerializer<>(this.getClass().getClassLoader()))
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofHours(24)))
.build();
//获取最终打包jar所在位置
String path = new ApplicationHome(getClass()).getSource().getParentFile().toString();
System.out.println(path);
//以下各行配置分别对应
//配置硬盘缓存存储地址
//最大对象默认大小
//缓存记录最大条数
CacheManager ehcacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence(new File(path, "ehcache")))
.withDefaultSizeOfMaxObjectSize(500, MemoryUnit.KB)
.withDefaultSizeOfMaxObjectGraph(2000)
.withCache(CACHE_NAME_USER , cacheConfiguration)
.withCache(CACHE_NAME_SYS, cacheConfiguration)
.build(true);
return ehcacheManager;
}
}
三、使用时以注解方式使用
- @Cacheable:可用于类或方法上;在目标方法执行前,会根据key先去缓存中查询看是否有数据,有就直接返回缓存中的key对应的value值。不再执行目标方法;无则执行目标方法,并将方法的返回值作为value,并以键值对的形式存入缓存
- @CachePut:可用于类或方法上;在执行完目标方法后,并将方法的返回值作为value,并以键值对的形式存入缓存中
- @CacheEvict:可用于类或方法上;在执行完目标方法后,清除缓存中对应key的数据(如果缓存中有对应key的数据缓存的话)
- @CacheConfig(cacheNames = “cache-user”)全局配置,复杂配置。
例:@CacheConfig(cacheNames = “cache-user”)指定使用cache-user缓存。
总结
提示:上述使用发现在打包时,会出现jar路径null异常,pom添加配置禁用测试
<properties>
<skipTests>true</skipTests>
</properties>