Cache eviction removes stale entries so a cache can limit retained data instead of growing until memory usage becomes unsafe. Time-based eviction is useful when a lazily populated cache serves data that may remain unused, especially across larger dictionaries or multiple languages. section 7.4 section 7.4.1
Treat eviction as an operational decision rather than an arbitrary timeout:
- Estimate an initial duration from the expected traffic pattern, service-level objectives, and request rate when production data is unavailable. section 7.4
- Derive intervals between requests from logs, construct their statistical distribution, and choose a percentile such as the 90th or 99th as a candidate duration. section 7.4
- Record cache hits, misses, and related statistics after deployment; increase the duration when the miss ratio is high. section 7.4
- Preserve tracing and metrics, and prefer established cache implementations such as Google Guava, Caffeine, or Spring cache rather than expanding manual eviction code. section 7.4.1
A word-existence cache
A loading cache can use expire-after-access with a default eviction time of five minutes, record statistics, and call the existing word-existence method on a miss. The duration remains a tunable default rather than a permanent assumption. section 7.4
Listing 7.18 Constructing a word-exists cache
LoadingCache<String, Boolean> wordExistsCache =
CacheBuilder.newBuilder()
.ticker(ticker)
.expireAfterAccess(DEFAULT_EVICTION_TIME).recordStats() #A
.build(
new CacheLoader<String, Boolean>() {
@Override
public Boolean load(@Nullable String word) throws Exception {
if (word == null)
return false; #B
}
return checkIfWordExists(word); #C
}
});Shows a loading cache combining access-based expiration, statistics recording, null handling, and miss-time loading. section 7.4


