How to get all values from guava LoadingCache without passing any keys

大城市里の小女人 提交于 2019-12-23 08:03:31

问题


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<?> keys) or getAllPresent(Iterable<?> keys) methods are there but those are expecting the keys to be passed.


回答1:


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

You can view any Cache as a ConcurrentMap using its asMap view, but how the asMap view interacts with the Cache requires some explanation.

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


来源:https://stackoverflow.com/questions/49200531/how-to-get-all-values-from-guava-loadingcache-without-passing-any-keys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!