From f1f9c5a4880eb9a125e64a176625561ce682900d Mon Sep 17 00:00:00 2001 From: rlaehd62 Date: Mon, 18 Nov 2024 02:40:07 +0900 Subject: [PATCH] refactor: replace hardcoded values with constants --- .../springframework/util/ConcurrentLruCache.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/ConcurrentLruCache.java b/spring-core/src/main/java/org/springframework/util/ConcurrentLruCache.java index 87456ae0549..2c7f4541ab6 100644 --- a/spring-core/src/main/java/org/springframework/util/ConcurrentLruCache.java +++ b/spring-core/src/main/java/org/springframework/util/ConcurrentLruCache.java @@ -50,6 +50,10 @@ public final class ConcurrentLruCache { private final int capacity; + private final int DEFAULT_CAPACITY = 16; + + private final float DEFAULT_LOAD_FACTOR = 0.75f; + private final AtomicInteger currentSize = new AtomicInteger(); private final ConcurrentMap> cache; @@ -62,6 +66,7 @@ public final class ConcurrentLruCache { private final Lock evictionLock = new ReentrantLock(); + /* * Queue that contains all ACTIVE cache entries, ordered with least recently used entries first. * Read and write operations are buffered and periodically processed to reorder the queue. @@ -83,7 +88,7 @@ public ConcurrentLruCache(int capacity, Function generator) { private ConcurrentLruCache(int capacity, Function generator, int concurrencyLevel) { Assert.isTrue(capacity >= 0, "Capacity must be >= 0"); this.capacity = capacity; - this.cache = new ConcurrentHashMap<>(16, 0.75f, concurrencyLevel); + this.cache = new ConcurrentHashMap<>(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, concurrencyLevel); this.generator = generator; this.readOperations = new ReadOperations<>(this.evictionQueue); this.writeOperations = new WriteOperations(); @@ -274,11 +279,10 @@ public void run() { private void evictEntries() { while (currentSize.get() > capacity) { final Node node = evictionQueue.poll(); - if (node == null) { - return; + if (node != null) { + cache.remove(node.key, node); + markAsRemoved(node); } - cache.remove(node.key, node); - markAsRemoved(node); } }