LRU (Least Recently Used)
cache allows you to store memory bounded data with auto-eviction policy for least recently used entries once the maximum capacity is reached.
- Fixed Size
- Fast Read
- Fast Add
- Fast Updates
HashMap
is used for O(1) operations on get(key) and put(key, value).Doubly Linked List
is used because it supports fast insertion and deletion of nodes.
public LRU(int capacity)
public LRU(int capacity)
public void put(K key, V value)
public V get(K key)
public V remove(K key)
public List<V> getValues()
public Iterator<V> valueIterator()
private void addFront(Entry<K, V> entry)
private void moveFront(Entry<K, V> entry)
private void delete(Entry<K, V> entry)
private void ensureCapacity()
- Fast Access
- Fast Updates
- Space Heavy