From ae4969d27923d6b3fa086033d024d242fe58dc5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Porras=20Campo?= Date: Wed, 8 Jan 2025 11:36:10 +0100 Subject: [PATCH] feat: support weakKeys in the CacheConfiguration --- .../tools/ddk/caching/CacheConfiguration.java | 15 +++++++++++++++ .../com/avaloq/tools/ddk/caching/MapCache.java | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/com.avaloq.tools.ddk/src/com/avaloq/tools/ddk/caching/CacheConfiguration.java b/com.avaloq.tools.ddk/src/com/avaloq/tools/ddk/caching/CacheConfiguration.java index 11ede1687..d16b02d40 100644 --- a/com.avaloq.tools.ddk/src/com/avaloq/tools/ddk/caching/CacheConfiguration.java +++ b/com.avaloq.tools.ddk/src/com/avaloq/tools/ddk/caching/CacheConfiguration.java @@ -18,6 +18,7 @@ public class CacheConfiguration { private boolean arraySize; private boolean softValues; + private boolean weakKeys; private boolean weakValues; private boolean statistics; @@ -45,6 +46,16 @@ public CacheConfiguration useWeakValues() { return this; } + /** + * All keys stored in the cache are wrapped as {@link java.lang.ref.SoftReference WeakReference}, allowing them to be garbage collected as necessary. + * + * @return the cache configuration + */ + public CacheConfiguration useWeakKeys() { + weakKeys = true; + return this; + } + /** * If enabled, the cache will treat the values as arrays and count their total entries when determining the maximum number of allowed entries. * @@ -85,6 +96,10 @@ public boolean isWeakValuesEnabled() { return weakValues; } + public boolean isWeakKeysEnabled() { + return weakKeys; + } + public boolean isStatisticsEnabled() { return statistics; } diff --git a/com.avaloq.tools.ddk/src/com/avaloq/tools/ddk/caching/MapCache.java b/com.avaloq.tools.ddk/src/com/avaloq/tools/ddk/caching/MapCache.java index 9ab32458b..acf8e041b 100644 --- a/com.avaloq.tools.ddk/src/com/avaloq/tools/ddk/caching/MapCache.java +++ b/com.avaloq.tools.ddk/src/com/avaloq/tools/ddk/caching/MapCache.java @@ -57,6 +57,11 @@ private CacheBuilder configure(final CacheConfiguration config) } else if (config.isWeakValuesEnabled()) { cacheBuilder.weakValues(); } + + if (config.isWeakKeysEnabled()) { + cacheBuilder.weakKeys(); + } + if (config.getInitialCapacity() >= 0) { cacheBuilder.initialCapacity(config.getInitialCapacity()); }