Skip to content

Adaptive Replacement Cache (ARC) #5116

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

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
092e69f
Create ARCCache.java
devxadarsh Apr 23, 2024
45fb360
Delete src/main/java/com/thealgorithms/datastructures/caches/ARCCache…
devxadarsh Apr 23, 2024
bfb26f6
Adaptive Replacement Cache (ARC)
devxadarsh Apr 23, 2024
4d5a396
Update ARCCache.java
devxadarsh Apr 23, 2024
5249c26
Added ARCCacheTest.java
devxadarsh Apr 23, 2024
afc2e19
Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache…
devxadarsh Apr 24, 2024
67e2053
Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache…
devxadarsh Apr 24, 2024
e27f0b8
Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache…
devxadarsh Apr 24, 2024
1589701
Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache…
devxadarsh Apr 24, 2024
600554c
Update ARCCache.java
devxadarsh Apr 24, 2024
fa53c3c
Update ARCCacheTest.java
devxadarsh Apr 24, 2024
91a9c9c
Merge branch 'TheAlgorithms:master' into master
devxadarsh May 1, 2024
c0b4e63
Merge branch 'master' into master
devxadarsh May 1, 2024
b70bf14
Merge branch 'TheAlgorithms:master' into master
devxadarsh May 8, 2024
75b9f7e
Updated capacity method
Adarshdm May 8, 2024
538fb49
Merge pull request #1 from devxadarsh/work
devxadarsh May 8, 2024
3007f2d
Updated capacity method
Adarshdm May 8, 2024
235471f
Merge pull request #2 from devxadarsh/work
devxadarsh May 8, 2024
3dbb1ae
Merge branch 'TheAlgorithms:master' into master
devxadarsh May 8, 2024
04be243
Updated Imports
devxadarsh May 8, 2024
5b68052
Merge branch 'master' into master
devxadarsh May 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.thealgorithms.datastructures.caches;

import java.util.LinkedHashMap;
import java.util.Map;
/**
* Adaptive Replacement Cache (ARC)
* <p>
* dynamically adjusts cache size based on recent access patterns.
* It aims to provide better performance compared to traditional caching algorithms
* like LRU (Least Recently Used) and LFU (Least Frequently Used).
* It combines elements of LRU (Least Recently Used) and LFU (Least Frequently Used) algorithms
* to efficiently manage frequently accessed and recently used items,
* optimizing cache performance in changing workload scenarios.
* <a href="https://en.wikipedia.org/wiki/Adaptive_replacement_cache">...</a>
* @author Adarsh Pandey (<a href="https://github.com/devxadarsh">...</a>)
*
* @param <K> key type
* @param <V> value type
*/

public class ARCCache<K, V> {
private final int capacity;
private final Map<K, V> cache;
private final LinkedHashMap<K, Integer> usageCounts;
private final int t1Capacity; // Capacity for the t1 cache
private final int b1Capacity; // Capacity for the b1 cache
private int totalCount;
/**
* Retrieves the value associated with the given key from the cache.
* If the key is present in the cache, its usage count is incremented.
*
* @param key the key whose associated value is to be retrieved
* @return the value associated with the key, or null if the key is not present in the cache
*/
public ARCCache(int capacity) {
this.capacity = capacity;
this.cache = new LinkedHashMap<>();
this.usageCounts = new LinkedHashMap<>();
this.t1Capacity = capacity / 2; // Capacity for the t1 cache
this.b1Capacity = capacity - t1Capacity; // Capacity for the b1 cache
this.totalCount = 0;
}

/**
* Retrieves the value associated with the given key from the cache.
* If the key is present in the cache, its usage count is incremented.
*
* @param key the key whose associated value is to be retrieved
* @return the value associated with the key, or null if the key is not present in the cache
*/
public V get(K key) {
if (cache.containsKey(key)) {
usageCounts.put(key, usageCounts.getOrDefault(key, 0) + 1);
return cache.get(key);
}
return null;
}

/**
* Adds the specified key-value pair to the cache.
* If the cache exceeds its capacity after adding the new entry, eviction is performed.
* Updates the usage count for the added key.
*
* @param key the key with which the specified value is to be associated
* @param value the value to be associated with the specified key
*/
public void put(K key, V value) {
if (cache.size() >= capacity) {
evict();
}
cache.put(key, value);
usageCounts.put(key, 1);
totalCount++;
}

/**
* Evicts an item from the cache when it exceeds its capacity.
* Implements the Adaptive Replacement Cache (ARC) algorithm logic for eviction.
* Removes the least recently used item based on its usage count.
*/
private void evict() {
if (!cache.isEmpty()) {
K keyToRemove = null;
int minUsageCount = Integer.MAX_VALUE;
for (Map.Entry<K, Integer> entry : usageCounts.entrySet()) {
if (entry.getValue() < minUsageCount) {
keyToRemove = entry.getKey();
minUsageCount = entry.getValue();
}
}
cache.remove(keyToRemove);
usageCounts.remove(keyToRemove);
totalCount--;
adjustCacheSize();
}
}

/**
* Adjust the cache sizes based on t1capacity and b1capacity after eviction from cache
*/
private void adjustCacheSize() {
if (cache.size() > capacity) {
int excess = cache.size() - capacity;
int t1Size = cache.size() - b1Capacity;
while (excess > 0 && !cache.isEmpty()) {
K keyToRemove = usageCounts.keySet().iterator().next();
if (t1Size > t1Capacity || (t1Size > 0 && usageCounts.get(keyToRemove) > 1)) {
cache.remove(keyToRemove);
usageCounts.remove(keyToRemove);
totalCount--;
if (t1Size > 0) {
t1Size--;
}
} else {
cache.remove(keyToRemove);
usageCounts.remove(keyToRemove);
totalCount--;
}
excess--;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.thealgorithms.datastructures.caches;

import static org.junit.jupiter.api.Assertions.*;

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

public class ARCCacheTest {
private ARCCache<Integer, String> cache;

@BeforeEach
public void setUp() {
int t1Capacity = 2;
int b1Capacity = 1;
int totalCapacity = t1Capacity + b1Capacity;
cache = new ARCCache<>(totalCapacity); // Set capacity to 3 for testing purposes
}

@Test
public void testPutAndGet() {
cache.put(1, "Value1");
cache.put(2, "Value2");
cache.put(3, "Value3");

assertEquals("Value1", cache.get(1));
assertEquals("Value2", cache.get(2));
assertEquals("Value3", cache.get(3));
}

@Test
public void testEviction() {
cache.put(1, "Value1");
cache.put(2, "Value2");
cache.put(3, "Value3");

cache.put(4, "Value4"); // This should evict key 1

assertNull(cache.get(1)); // Key 1 should have been evicted
assertEquals("Value2", cache.get(2)); // Other keys should still be present
assertEquals("Value3", cache.get(3));
assertEquals("Value4", cache.get(4));
}

@Test
public void nullKeysAndValues() {
cache.put(null, "Value1");
cache.put(2, null);

assertEquals("Value1", cache.get(null));
assertNull(cache.get(2));
assertNull(cache.get(6));
}

@Test
public void testRepeatedGet() {
cache.put(1, "Value1");

// Repeated get calls should not affect eviction
cache.get(1);
cache.get(1);
cache.get(1);

// Adding new elements should still evict old ones
cache.put(2, "Value2");
cache.put(3, "Value3");
cache.put(4, "Value4");

assertNull(cache.get(2)); // Key 2 should have been evicted
assertEquals("Value1", cache.get(1)); // Other keys should still be present
assertEquals("Value3", cache.get(3)); // Other keys should still be present
}
}