ehcache3 持久化缓存说明
所属分类 ehcache
浏览量 268
https://www.ehcache.org/documentation/3.8/getting-started.html
// Only 10 entries allowed on heap. Eviction will occur when full.
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES);
// A shortcut to specify 10 entries.
ResourcePoolsBuilder.heap(10);
// Only 10 MB allowed. Eviction will occur when full.
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, MemoryUnit.MB);
For every tier except the heap tier, calculating the size of the cache is fairly easy.
You more or less sum the size of all byte buffers containing the serialized entries.
When heap is limited by size instead of entries, it is a bit more complicated.
Byte sizing has a runtime performance impact that depends on the size and graph complexity of the data cached.
堆上缓存对象大小计算比较复杂
持久化缓存
final String CACHE1 = "cache1";
boolean persistent = false;
PersistentCacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence("d:/ehcache/diskdemo/"))
.withCache(CACHE1,
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
//.heap(10, EntryUnit.ENTRIES)
//.offheap(1, MemoryUnit.MB)
// persistent
.disk(20, MemoryUnit.MB, persistent)
)
).build(true);
Cache< String,String > cache = cacheManager.getCache(CACHE1, String.class, String.class);
System.out.println("persistent="+persistent);
String key = "key1";
String value = cache.get(key);
// persistent 为 false , close之后 不会保存 ,value为 null
System.out.println("value.1="+value);
value = LocalDateTime.now().toString();
cache.put(key,value);
value = cache.get(key);
System.out.println("value.2="+value);
// persistent 为 true 时,保存缓存到磁盘
cacheManager.close();
If you wish to use disk storage (like for persistent Cache instances), you’ll have to provide a location where data should be stored on disk to the CacheManagerBuilder.persistence() static method.
You define a resource pool for the heap. This will be your faster but smaller pool.
You define a resource pool for the off-heap. Still pretty fast and a bit bigger.
You define a persistent resource pool for the disk. It is persistent because said it should be (last parameter is true).
All values stored in the cache will be available after a JVM restart (assuming the CacheManager has been closed cleanly by calling close())
上一篇
下一篇
javascript 字符串相等判断
javascript 实用代码
ehcache 例子
ehcache3 过期测试
vertx架构简介
CompletableFuture 例子