Skip to content

Commit

Permalink
refactor: replace hardcoded values with constants
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaehd62 committed Nov 17, 2024
1 parent e51bf3e commit f1f9c5a
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public final class ConcurrentLruCache<K, V> {

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<K, Node<K, V>> cache;
Expand All @@ -62,6 +66,7 @@ public final class ConcurrentLruCache<K, V> {

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.
Expand All @@ -83,7 +88,7 @@ public ConcurrentLruCache(int capacity, Function<K, V> generator) {
private ConcurrentLruCache(int capacity, Function<K, V> 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();
Expand Down Expand Up @@ -274,11 +279,10 @@ public void run() {
private void evictEntries() {
while (currentSize.get() > capacity) {
final Node<K, V> node = evictionQueue.poll();
if (node == null) {
return;
if (node != null) {
cache.remove(node.key, node);
markAsRemoved(node);
}
cache.remove(node.key, node);
markAsRemoved(node);
}
}

Expand Down

0 comments on commit f1f9c5a

Please sign in to comment.