guava工具之cache缓存


1、pom


    com.google.guava
    guava
    23.0

2、cache

// 方式一
LoadingCache cache = CacheBuilder.newBuilder()     
    .maximumSize(10000)      // 大小
    .expireAfterWrite(10, TimeUnit.SECONDS)      // 过期时间
    .build(new CacheLoader() {         
        @Override         
        public Graph load(Graph o) throws Exception {             
            return createExpensiveGraph(key);         
        }     
    }); 
Object value = cache.get("key");

// 方式2 
Cache cache = CacheBuilder.newBuilder().maximumSize(1000).build(); 
Object value = cache.get("key", new Callable() {     
    public Object call() {         
        createExpensiveGraph(key);     
    } 
});
						  
					  
						
							

相关