diff options
| author | Roman / Linnea Gräf <roman.graef@gmail.com> | 2023-02-24 15:17:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-24 15:17:10 +0100 |
| commit | df945f9d58d4f40f9adc4727c4f8d548b21fa4b0 (patch) | |
| tree | 36a7e6b484152aadb91e03cd968a78d956b6129b /src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java | |
| parent | f72bfdd939a805cabfe64f3f3238ca45375fd1f8 (diff) | |
| download | notenoughupdates-df945f9d58d4f40f9adc4727c4f8d548b21fa4b0.tar.gz notenoughupdates-df945f9d58d4f40f9adc4727c4f8d548b21fa4b0.tar.bz2 notenoughupdates-df945f9d58d4f40f9adc4727c4f8d548b21fa4b0.zip | |
Museum: Display hydrated items for items taken outside of the repo (#621)
Co-authored-by: Lulonaut <lulonaut@tutanota.de>
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java b/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java index f107d522..6adbc30d 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/LRUCache.java @@ -32,13 +32,15 @@ public interface LRUCache<K, V> extends Function<K, V> { } static <K, V> LRUCache<K, V> memoize(Function<K, V> mapper, IntSupplier maxCacheSize) { - Map<K, V> cache = new LinkedHashMap<K, V>(10, 0.75F, true) { + Map<K, Object> cache = new LinkedHashMap<K, Object>(10, 0.75F, true) { @Override - protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { + protected boolean removeEldestEntry(Map.Entry<K, Object> eldest) { return this.size() > maxCacheSize.getAsInt(); } }; - Map<K, V> synchronizedCache = Collections.synchronizedMap(cache); + Object SENTINEL_CACHE_RESULT_NULL = new Object(); + Function<K, Object> sentinelAwareMapper = mapper.andThen(it -> it == null ? SENTINEL_CACHE_RESULT_NULL : it); + Map<K, Object> synchronizedCache = Collections.synchronizedMap(cache); return new LRUCache<K, V>() { @Override public void clearCache() { @@ -52,7 +54,8 @@ public interface LRUCache<K, V> extends Function<K, V> { @Override public V apply(K k) { - return synchronizedCache.computeIfAbsent(k, mapper); + Object value = synchronizedCache.computeIfAbsent(k, sentinelAwareMapper); + return value == SENTINEL_CACHE_RESULT_NULL ? null : (V) value; } }; } |
