首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

google guava cache 处置

2012-09-13 
google guava cache 处理使用本地内存?直接操作cache使用cache.put(key,value), 同时可以使用Cache.asMap(

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(), 清除的个数

?

?

热点排行