Is there any such thing as a combination of Guava's Cache
and Multimap
functionality available? Essentially, I need a collection where entries expire after a given time such as available in Cache
but I have non-unique keys and I need the entries to expire independently.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
As long as you're talking about
Cache
and notLoadingCache
you could pass theCache.asMap()
view intoMultimaps.newMultimap
.With a Guava Cache there is no put method, the cache is designed to be self-populating. The values returned from a key lookup are calculated at runtime. A similar approach is taken by Commons Collections Transformer Factories.
I think you could implement what you are looking for quite easily. If you look at a simple Map backed example such as Kitty-Cache you can see that you could replace the Map with a Multimap and rewrite the other methods accordingly. So in KittyCache.java internally you could have something like:
The trick for this kind of cache is that nothing really expires until someone requests it.
I think that Louis Wasserman provided the answer in one of the comments above, i.e. that there is no off-the-shelf combo of
Multimap
andCache
available. I have solved my problem/requirements with the solution outlined in pseudo-code below:This simple 'solution' has some limitations but it works OK for me.