#2677 - Change trimOnPut to be 10% of max or 1_000.

Can look to provide an explicit option via `@CacheBeanTuning` later.
This commit is contained in:
Rob Bygrave committed 2022-05-02 22:19:37 +12:00
1 parent 5c332286b6
commit 2cf83278ea
3 files changed
+37 -14

No files matched your search

@@ -49,6 +49,7 @@ public class DefaultServerCache implements ServerCache {
protected final int trimFrequency;
protected final int maxIdleSecs;
protected final int maxSecsToLive;
protected final long trimOnPut;
protected final ReentrantLock lock = new ReentrantLock();
protected final AtomicLong mutationCounter = new AtomicLong();
@@ -60,6 +61,7 @@ public class DefaultServerCache implements ServerCache {
this.maxIdleSecs = config.getMaxIdleSecs();
this.maxSecsToLive = config.getMaxSecsToLive();
this.trimFrequency = config.determineTrimFrequency();
this.trimOnPut = config.determineTrimOnPut();
MetricFactory factory = MetricFactory.get();
String prefix = "l2n.";
@@ -191,7 +193,7 @@ public class DefaultServerCache implements ServerCache {
public void put(Object key, Object value) {
map.put(key, new SoftReference<>(new CacheEntry(key, value)));
putCount.increment();
if (mutationCounter.incrementAndGet() > 1000) {
if (mutationCounter.incrementAndGet() > trimOnPut) {
runEviction();
}
}
@@ -76,4 +76,14 @@ public final class DefaultServerCacheConfig {
}
return 0;
}
/**
* Determine the number of mutations/puts required to trigger a runEviction() in the foreground.
*/
public long determineTrimOnPut() {
if (maxSize > 0) {
return maxSize / 10;
}
return 1000;
}
}