本文介绍了如何在不传递任何键的情况下从guava LoadingCache获取所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Guava LoadingCache来缓存一些结果。使用加载方法,我从其他来源获取结果,并使用 put(key,value)放入缓存。
现在我要解决的问题是:我想在不传递任何键的情况下获得该缓存中的所有可用结果。因为我有兴趣获取当时在缓存中显示的所有值,而不管任何特定键。

I am using Guava LoadingCache to cache some of the results. Using load method I fetch results from other source and put into cache using 'put(key,value)'.Now the problem I am trying to solve is: I want to get all the available results in that cache with out passing any keys. Because I am interested in taking all the values presented in the cache at that time regardless of any specific keys.

getall(Iterable< ;?键) getAllPresent(Iterable<?>键)方法,但是那些方法期望键可以通过。

getall(Iterable<?> keys) or getAllPresent(Iterable<?> keys) methods are there but those are expecting the keys to be passed.

推荐答案

您可以使用查看并操作返回的 ConcurrentMap 。番石榴:

You can use (Loading)Cache#asMap view and operate on returned ConcurrentMap. There's nice description on Guava wiki page:


  • cache.asMap()包含所有内容
    缓存中当前加载的条目。因此,例如, cache.asMap()。keySet()包含当前所有已加载的
    密钥。

  • cache.asMap() contains all entries that are currently loaded in the cache. So, for example, cache.asMap().keySet() contains all the currently loaded keys.

这篇关于如何在不传递任何键的情况下从guava LoadingCache获取所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 12:59