A cache implementing a least recently used policy.
| 23 | * A cache implementing a least recently used policy. |
| 24 | */ |
| 25 | public class LRUCache<K, V> implements Cache<K, V> { |
| 26 | private final LinkedHashMap<K, V> cache; |
| 27 | |
| 28 | public LRUCache(final int maxSize) { |
| 29 | cache = new LinkedHashMap<>(16, .75f, true) { |
| 30 | @Override |
| 31 | protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { |
| 32 | return this.size() > maxSize; // require this. prefix to make lgtm.com happy |
| 33 | } |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public V get(K key) { |
| 39 | return cache.get(key); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void put(K key, V value) { |
| 44 | cache.put(key, value); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public boolean remove(K key) { |
| 49 | return cache.remove(key) != null; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public long size() { |
| 54 | return cache.size(); |
| 55 | } |
| 56 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…