首页  

ehcache 例子     所属分类 ehcache 浏览量 261
ehcache3.8.1

timeToLiveSeconds TTL 自创建时间起多少秒后失效
timeToIdleSeconds TTI 自最后读取或更新起多少秒后失效

过期事件 读取时触发 ?
EVICTED,key0,EVICTED on org.ehcache.core.Ehcache@7dc222ae key,oldValue,newValue='key0','key0_2024-01-18T19:55:25.431','null'
EXPIRED,key5,EXPIRED on org.ehcache.core.Ehcache@7dc222ae key,oldValue,newValue='key5','key5_2024-01-18T19:55:25.440','null'

三种组合方式
heap + off-heap
heap + disk
heap + off-heap + disk

heap + off-heap + disk 组合使用时
数据先落到堆内内存,同时同步到堆外内存以及本地磁盘,本地磁盘数据是最全的。
空间大小必须 disk > off-heap > heap
读取时  查找顺序 heap off-heap disk  



import org.ehcache.Cache; import org.ehcache.CacheManager; import org.ehcache.config.CacheConfiguration; import org.ehcache.config.builders.*; import org.ehcache.config.units.EntryUnit; import org.ehcache.config.units.MemoryUnit; import org.ehcache.event.CacheEvent; import org.ehcache.event.CacheEventListener; import org.ehcache.event.EventType; import java.time.Duration; import java.time.LocalDateTime; public class EchcacheConfigDemo { private static final String CACHE1 = "cache1"; public static void main(String[] args) throws Exception { ResourcePoolsBuilder resourcePoolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder() .heap(3, EntryUnit.ENTRIES) // .offheap(1, MemoryUnit.MB) //.disk(10, MemoryUnit.MB,false) ; CacheConfiguration< String,String > config = CacheConfigurationBuilder .newCacheConfigurationBuilder(String.class, String.class, resourcePoolsBuilder) // 缓存超时时间 .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(5))) .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(3))) // 统计对象大小时对象图遍历深度 .withSizeOfMaxObjectGraph(100) //可缓存的最大对象大小 .withSizeOfMaxObjectSize(1, MemoryUnit.MB) // 添加监听器 .add(CacheEventListenerConfigurationBuilder.newEventListenerConfiguration( new EhCacheEventListener(), EventType.EXPIRED,EventType.EVICTED) .unordered() .asynchronous() //.ordered() //.synchronous() ) .build(); // 根据配置建立缓存管理器 CacheManager cacheManager = CacheManagerBuilder .newCacheManagerBuilder() .with(CacheManagerBuilder.persistence("d:/ehcache/configdemo/")) .withCache("cache1", config) .build(true); Cache< String, String> cache1 = cacheManager.getCache(CACHE1, String.class, String.class); int num = 9; System.out.println("cache write start,num="+num); for(int i=0;i < num;i++){ String key = "key"+i; String value = key+"_"+ LocalDateTime.now(); cache1.put(key,value); } System.out.println("cache write end,"+num); Thread.sleep(10*1000L); System.out.println("cache get start,"+num); for(int i=0;i < num;i++){ String key = "key"+i; System.out.println(key+"="+cache1.get(key)); } System.out.println("cache get done,"+num); Thread.sleep(100*1000L); System.out.println(); } private static class EhCacheEventListener implements CacheEventListener { public void onEvent(CacheEvent event){ System.out.println(event.getType()+","+event.getKey()+","+event); } } }

上一篇     下一篇
java8 JavaScript引擎 Nashorn

javascript 字符串相等判断

javascript 实用代码

ehcache3 持久化缓存说明

ehcache3 过期测试

vertx架构简介