Skip to content

RR cache #6307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 30, 2025
Prev Previous commit
Next Next commit
Fixes
  • Loading branch information
KevinMwita7 committed Jun 20, 2025
commit 25427f31988461dd99bbae6fbcd428cd9091dc2c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private void evictExpired() {
while (it.hasNext()) {
K k = it.next();
CacheEntry<V> entry = cache.get(k);
if (entry.isExpired()) {
if (entry != null && entry.isExpired()) {
it.remove();
cache.remove(k);
notifyEviction(k, entry.value);
Expand Down Expand Up @@ -248,7 +248,11 @@ private void notifyEviction(K key, V value) {
*/
public long getHits() {
lock.lock();
try { return hits; } finally { lock.unlock(); }
try {
return hits;
} finally {
lock.unlock();
}
}

/**
Expand All @@ -258,7 +262,11 @@ public long getHits() {
*/
public long getMisses() {
lock.lock();
try { return misses; } finally { lock.unlock(); }
try {
return misses;
} finally {
lock.unlock();
}
}

/**
Expand Down Expand Up @@ -300,8 +308,7 @@ public String toString() {
visible.put(entry.getKey(), entry.getValue().value);
}
}
return String.format("Cache(capacity=%d, size=%d, hits=%d, misses=%d, entries=%s)",
capacity, visible.size(), hits, misses, visible);
return String.format("Cache(capacity=%d, size=%d, hits=%d, misses=%d, entries=%s)", capacity, visible.size(), hits, misses, visible);
} finally {
lock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
package com.thealgorithms.datastructures.caches;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

class RRCacheTest {

private RRCache<String, String> cache;
private List<String> evictedKeys;
private Set<String> evictedKeys;
private List<String> evictedValues;

@BeforeEach
void setUp() {
evictedKeys = new ArrayList<>();
evictedKeys = new HashSet<>();
evictedValues = new ArrayList<>();

cache = new RRCache.Builder<String, String>(3)
.defaultTTL(1000)
.random(new Random(0))
.evictionListener((k, v) -> {
evictedKeys.add(k);
evictedValues.add(v);
})
.build();
.defaultTTL(1000)
.random(new Random(0))
.evictionListener((k, v) -> {
evictedKeys.add(k);
evictedValues.add(v);
})
.build();
}

@Test
Expand Down Expand Up @@ -148,11 +149,7 @@ void testBuilderNullEvictionListenerThrows() {

@Test
void testEvictionListenerExceptionDoesNotCrash() {
RRCache<String, String> listenerCache = new RRCache.Builder<String, String>(1)
.evictionListener((k, v) -> {
throw new RuntimeException("Exception");
})
.build();
RRCache<String, String> listenerCache = new RRCache.Builder<String, String>(1).evictionListener((k, v) -> { throw new RuntimeException("Exception"); }).build();

listenerCache.put("a", "a");
listenerCache.put("b", "b"); // causes eviction but should not crash
Expand All @@ -161,9 +158,7 @@ void testEvictionListenerExceptionDoesNotCrash() {

@Test
void testTtlZeroThrowsIllegalArgumentException() {
Executable exec = () -> new RRCache.Builder<String, String>(3)
.defaultTTL(-1)
.build();
Executable exec = () -> new RRCache.Builder<String, String>(3).defaultTTL(-1).build();
assertThrows(IllegalArgumentException.class, exec);
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy