Basic implementation of a generic N-way Set Associative Cache using Java
Comes with 3 basic Cache Replacement policy implementations:
To run the test suite - just go into the project root and type ./gradlew test
To use in your code, NWaySetAssociativeCache
is supplied with Set Size, Entry Size and a Replacement Algorithm:
NWaySetAssociativeCache(int setSize,
int entrySize,
ReplacementAlgorithm<K, V, M> replacementAlgorithm)
Example:
Cache<Integer, String, Long> cache =
new NWaySetAssociativeCache<>(8, 2, new LruReplacementAlgorithm<>());
cache.put(16, "Good");
cache.put(16, "Bad");
String value = cache.get(16); // value == "Bad"
To use a custom Cache Replacement Algorithm, implement the ReplacementAlgorithm
interface and supply it into the NWaySetAssociativeCache
:
public interface ReplacementAlgorithm<K,V,M>
extends Comparator<CacheElement<K,V,M>>
To integrate with other typical caches, NWaySetAssociativeCache
implements a basic Cache
interface:
public interface Cache<K, V, M> {
V get(final K key);
void put(final K key, final V value);
}