Good day ! Can I mix Time-based and Reference-based stategy ? I want cache based on SoftReference with 5 minute lifecycles, can I get it from box ?
Asked
Active
Viewed 47 times
0
-
Welcome to Stack Overflow. Please read up on how to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) You may also benefit from [How to ask](https://stackoverflow.com/help/how-to-ask) and [What is expected of SO users](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – mtr.web Apr 11 '18 at 14:47
1 Answers
1
You can compose most features together, unless documented as incompatible. In those cases an exception will be thrown by the builder.
Cache<K, V> cache = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES) // or Duration.ofMinutes(5)
.softValues()
.build();
Please be aware that soft references have a performance impact on the garbage collector and can be finicky. A maximum size is generally preferred, all else being equal.

Ben Manes
- 9,178
- 3
- 35
- 39