首页  

ehcache3 过期测试     所属分类 ehcache 浏览量 310
timeToLiveSeconds TTL 自创建时间起多少秒后失效
timeToIdleSeconds TTI 自最后读取或更新起多少秒后失效

put   更新 TTL 
put get 更新 TTI 
containsKey 不会 更新 TTL 和 TTI


import dyyx.util.CommUtil; import org.ehcache.Cache; import org.ehcache.CacheManager; import org.ehcache.config.CacheConfiguration; import org.ehcache.config.ResourcePools; 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.EntryUnit; import java.time.Duration; import java.time.LocalDateTime; public class ExpireDemo { public static void main(String[] args) throws Exception { final String CACHE1 = "cache1"; final String CACHE2 = "cache2"; // 资源池 ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder() .heap(100, EntryUnit.ENTRIES) //.offheap(1, MemoryUnit.MB) // persistent true or false //.disk(20, MemoryUnit.MB, persistent) .build(); // 缓存1配置 CacheConfiguration config = CacheConfigurationBuilder .newCacheConfigurationBuilder(String.class, String.class,resourcePools) .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(2))) //.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(3))) .build(); // 缓存2配置 CacheConfiguration config2 = CacheConfigurationBuilder .newCacheConfigurationBuilder(String.class, String.class,resourcePools) //.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(2))) .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(2))) .build(); // CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true); // 创建缓存1 Cache< String,String > cache = cacheManager.createCache(CACHE1,config); // 需要先创建 // Cache< String,String > cache = cacheManager.getCache(CACHE1,String.class,String.class); // 创建缓存2 Cache< String,String > cache2 = cacheManager.createCache(CACHE2,config2); System.out.println("test for cache1 TTL"); final String key = "key1"; String value = cache.get(key); System.out.println("value.1="+value); value = LocalDateTime.now().toString(); cache.put(key,value); value = cache.get(key); System.out.println("value.2="+value); fetchWhenNull(cache,key); value = LocalDateTime.now().toString(); cache.put(key,value); System.out.println("value.3="+value); CommUtil.doSleep(1000); value = LocalDateTime.now().toString(); // 写入 重新设置 TTL cache.put(key,value); System.out.println("value.4="+value); fetchWhenNull(cache,key); System.out.println("\n\n\n test for cache2 TTI"); value = cache2.get(key); System.out.println("value.1="+value); value = LocalDateTime.now().toString(); cache2.put(key,value); value = cache2.get(key); System.out.println("value.2="+value); // 2秒后过期 fetchWhenNull(cache2,key); value = LocalDateTime.now().toString(); cache2.put(key,value); System.out.println("value.3="+value); CommUtil.doSleep(1000); value = LocalDateTime.now().toString(); // 写入 重新设置 TTI cache2.put(key,value); System.out.println("value.4="+value); // 两秒后过期 fetchWhenNull(cache2,key); value = LocalDateTime.now().toString(); cache2.put(key,value); System.out.println("value.5="+value); CommUtil.doSleep(1000); // get 更新 TTI value = cache2.get(key); System.out.println("value.6="+value); fetchWhenNull(cache2,key); cacheManager.close(); } private static void fetchWhenNull(Cache< String,String > cache,String key){ while(true){ // cache.get(key) 不能用 get ,会更新TTI if (cache.containsKey(key) == false) { System.out.println(LocalDateTime.now()+",null,key="+key); break; } CommUtil.doSleep(10); } } }

上一篇     下一篇
javascript 实用代码

ehcache 例子

ehcache3 持久化缓存说明

vertx架构简介

CompletableFuture 例子

CompletionStage 说明