|
| 1 | +package cchesser.javaperf.workshop.cache; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.util.Map.Entry; |
| 5 | +import java.util.stream.Collectors; |
| 6 | + |
| 7 | +/** |
| 8 | + * An LFU Cache with history tracking, modified |
| 9 | + * from Stack Overflow: https://stackoverflow.com/a/23668899 (used as a basis). |
| 10 | + * |
| 11 | + * @author JMonterrubio |
| 12 | + */ |
| 13 | +public class HistoricalCache<K, V> { |
| 14 | + |
| 15 | + private Map<K, CacheEntry<V>> innerCache = new LinkedHashMap<>(); |
| 16 | + private int cacheLimit; |
| 17 | + |
| 18 | + private Deque<String> historyLog = new LinkedList<>(); |
| 19 | + private int historyLimit; |
| 20 | + |
| 21 | + public HistoricalCache(int cacheLimit, int historyLimit) { |
| 22 | + this.cacheLimit = cacheLimit; |
| 23 | + this.historyLimit = historyLimit; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Stores the given key value pair in the cache |
| 28 | + * |
| 29 | + * @param key the unique identifier associated with the given value |
| 30 | + * @param value the value mapped to the given key |
| 31 | + */ |
| 32 | + public void store(K key, V value) { |
| 33 | + if (isFull()) { |
| 34 | + K keyToRemove = getLFUKey(); |
| 35 | + HistoricalCache<K, V>.CacheEntry<V> removedEntry = innerCache.remove(keyToRemove); |
| 36 | + |
| 37 | + if (historyLog.size() >= historyLimit) { |
| 38 | + historyLog.removeLast(); |
| 39 | + } |
| 40 | + historyLog.add(removedEntry.toString()); |
| 41 | + } |
| 42 | + |
| 43 | + CacheEntry<V> newEntry = new CacheEntry<V>(); |
| 44 | + newEntry.data = value; |
| 45 | + newEntry.frequency = 0; |
| 46 | + |
| 47 | + innerCache.put(key, newEntry); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Retrieves the value associated with the given key from the cache. |
| 52 | + * |
| 53 | + * @param key the key that maps to that value |
| 54 | + * @return the value mapped to the key |
| 55 | + * @throws NullPointerException if the key doesn't exist |
| 56 | + */ |
| 57 | + public V fetch(K key) { |
| 58 | + if (innerCache.containsKey(key)) { |
| 59 | + innerCache.get(key).frequency++; |
| 60 | + } |
| 61 | + |
| 62 | + return innerCache.get(key).data; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Indicates whether a key exists in the cache or not |
| 67 | + * |
| 68 | + * @param key the key to check |
| 69 | + * @return <code>true</code> if the key is in the cache, <code>false</code> |
| 70 | + * otherwise. |
| 71 | + */ |
| 72 | + public boolean exists(K key) { |
| 73 | + return innerCache.containsKey(key); |
| 74 | + } |
| 75 | + |
| 76 | + private boolean isFull() { |
| 77 | + return innerCache.size() == cacheLimit; |
| 78 | + } |
| 79 | + |
| 80 | + private K getLFUKey() { |
| 81 | + Optional<Entry<K, HistoricalCache<K, V>.CacheEntry<V>>> lfuEntry = innerCache.entrySet().stream() |
| 82 | + .collect(Collectors.minBy(Comparator.comparingInt(entry -> entry.getValue().frequency))); |
| 83 | + |
| 84 | + return lfuEntry.get().getKey(); |
| 85 | + } |
| 86 | + |
| 87 | + private class CacheEntry<T> { |
| 88 | + private T data; |
| 89 | + private int frequency; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments