Skip to content

Commit

Permalink
feat: support weakKeys in the CacheConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenporras committed Jan 8, 2025
1 parent 75a8a89 commit ae4969d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CacheConfiguration {

private boolean arraySize;
private boolean softValues;
private boolean weakKeys;
private boolean weakValues;
private boolean statistics;

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -85,6 +96,10 @@ public boolean isWeakValuesEnabled() {
return weakValues;
}

public boolean isWeakKeysEnabled() {
return weakKeys;
}

public boolean isStatisticsEnabled() {
return statistics;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ private CacheBuilder<Object, Object> configure(final CacheConfiguration config)
} else if (config.isWeakValuesEnabled()) {
cacheBuilder.weakValues();
}

if (config.isWeakKeysEnabled()) {
cacheBuilder.weakKeys();
}

if (config.getInitialCapacity() >= 0) {
cacheBuilder.initialCapacity(config.getInitialCapacity());
}
Expand Down

0 comments on commit ae4969d

Please sign in to comment.