参考资料
https://www.ehcache.org/documentation/
https://segmentfault.com/a/1190000021243491?utm_source=sf-similar-article
https://segmentfault.com/a/1190000013269653?utm_source=sf-similar-article
是个啥
Ehcache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
Ehcache 在应用程序中的位置:
主要的特性有:
1. 快速.
2. 简单.
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现
分层选项怎么弄
https://www.ehcache.org/documentation/3.8/tiering.html
怎么用
简单实例(测试下磁盘持久化)
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.8.1</version>
</dependency>
public class EhcaheManager {
static String getStoragePath(){
return "R:\\jclab\\us-ehcache\\src\\main\\resources\\ehcache-data";
}
public static void test(){
// 创建一个 含有名称为preConfigured的Cache(另有配置) 的CacheManager, but uninitialized
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence(new File(getStoragePath())))
.withCache("preConfigured",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.heap(10)))
// .withCache("preConfigured2",
// CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
// ResourcePoolsBuilder.heap(10)))
.build();
// 初始化 // or calling the CacheManagerBuilder.build(boolean init) method
cacheManager.init();
// You can create an XML file to configure a CacheManager.
// 根据 alias, key type and value type 获取Cache
Cache<Long, String> preConfigured =
cacheManager.getCache("preConfigured", Long.class, String.class);
// 另创建一个Cache
Cache<Long, String> myCache = cacheManager.createCache("myCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10,MemoryUnit.MB,true)));
myCache.put(1L, "da one!");
String value = myCache.get(1L);
cacheManager.removeCache("preConfigured");
cacheManager.close();
//Creating a cache manager with clustering support
}
public static void main(String[] args) {
test();
}
}
整合Springboot
https://segmentfault.com/a/1190000021243491?utm_source=sf-similar-article
- 配置
- 测试