Skip to content

Commit

Permalink
Merge pull request #1064 from rubenporras/weakKeys
Browse files Browse the repository at this point in the history
feat: support weakKeys in the CacheConfiguration
  • Loading branch information
rubenporras authored Jan 8, 2025
2 parents 75a8a89 + ae4969d commit acb87bd
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 acb87bd

Please sign in to comment.