google guava cache 处理
使用本地内存
?直接操作cache
使用cache.put(key,value), 同时可以使用Cache.asMap()来调用所以ConcurrentMap的方法来操作cache,但是通过asMap的数据不会自动loading到cache
?
三种清出cache的模式size-based eviction, time-based eviction, and reference-based eviction.
size-based:
// Some keys don't need refreshing, and we want refreshes to be done asynchronously.LoadingCache<Key, Graph> graphs = CacheBuilder.newBuilder() .maximumSize(1000) .refreshAfterWrite(1, TimeUnit.MINUTES) .build( new CacheLoader<Key, Graph>() { public Graph load(Key key) { // no checked exception return getGraphFromDatabase(key); } public ListenableFuture<Graph> reload(final Key key, Graph prevGraph) { if (neverNeedsRefresh(key)) { return Futures.immediateFuture(prevGraph); } else { // asynchronous! return ListenableFutureTask.create(new Callable<Graph>() { public Graph call() { return getGraphFromDatabase(key); } }); } } });??
?在refreshAfterWrite方法,会调用reload
?
数据
? 提供了一些数据采集的方法
? ?CacheBuilder.recordStats() 方法启动了 cache的数据收集
? ? Cache.stats() 返回了一个CacheStats对象, 提供一些数据方法
? ? hitRate(), 请求点击率
? ? averageLoadPenalty(), 加载new value,花费的时间, 单位nanosecondes
? ? evictionCount(), 清除的个数
?
?