-
Notifications
You must be signed in to change notification settings - Fork 21.2k
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
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
092e69f
Create ARCCache.java
devxadarsh 45fb360
Delete src/main/java/com/thealgorithms/datastructures/caches/ARCCache…
devxadarsh bfb26f6
Adaptive Replacement Cache (ARC)
devxadarsh 4d5a396
Update ARCCache.java
devxadarsh 5249c26
Added ARCCacheTest.java
devxadarsh afc2e19
Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache…
devxadarsh 67e2053
Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache…
devxadarsh e27f0b8
Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache…
devxadarsh 1589701
Update src/main/java/com/thealgorithms/datastructures/caches/ARCCache…
devxadarsh 600554c
Update ARCCache.java
devxadarsh fa53c3c
Update ARCCacheTest.java
devxadarsh 91a9c9c
Merge branch 'TheAlgorithms:master' into master
devxadarsh c0b4e63
Merge branch 'master' into master
devxadarsh b70bf14
Merge branch 'TheAlgorithms:master' into master
devxadarsh 75b9f7e
Updated capacity method
Adarshdm 538fb49
Merge pull request #1 from devxadarsh/work
devxadarsh 3007f2d
Updated capacity method
Adarshdm 235471f
Merge pull request #2 from devxadarsh/work
devxadarsh 3dbb1ae
Merge branch 'TheAlgorithms:master' into master
devxadarsh 04be243
Updated Imports
devxadarsh 5b68052
Merge branch 'master' into master
devxadarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
src/main/java/com/thealgorithms/datastructures/caches/ARCCache.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| 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 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; | ||
|
|
||
| /** | ||
| * This constructor initializes an ARCCache object with the given capacity and initializes other necessary fields | ||
| * @param capacity the initial capacity of the cache | ||
| * @throws IllegalArgumentException if the capacity is negative | ||
| */ | ||
| public ARCCache(int capacity) { | ||
|
vil02 marked this conversation as resolved.
|
||
| if (capacity < 0) { | ||
| throw new IllegalArgumentException("Capacity cannot be negative"); | ||
| } | ||
| 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; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the total capacity of the cache | ||
| * | ||
| * @return the total capacity of the cache | ||
| */ | ||
| private int capacity() { | ||
| return t1Capacity + b1Capacity; | ||
| } | ||
|
|
||
| /** | ||
| * 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--; | ||
| } | ||
| } | ||
| } | ||
| } | ||
73 changes: 73 additions & 0 deletions
73
src/test/java/com/thealgorithms/datastructures/caches/ARCCacheTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package com.thealgorithms.datastructures.caches; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| 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 | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.