|
| 1 | +package com.baeldung.lrucache; |
| 2 | + |
| 3 | +import java.util.Hashtable; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.concurrent.ConcurrentHashMap; |
| 7 | +import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 8 | + |
| 9 | +public class LRUCache<K, V> implements Cache<K, V> { |
| 10 | + private int size; |
| 11 | + private Map<K, LinkedListNode<CacheElement<K, V>>> linkedListNodeMap; |
| 12 | + private DoublyLinkedList<CacheElement<K, V>> doublyLinkedList; |
| 13 | + private ReentrantReadWriteLock.ReadLock readLock; |
| 14 | + private ReentrantReadWriteLock.WriteLock writeLock; |
| 15 | + |
| 16 | + public LRUCache(int size) { |
| 17 | + this.size = size; |
| 18 | + this.linkedListNodeMap = new Hashtable<>(size); |
| 19 | + this.doublyLinkedList = new DoublyLinkedList<>(); |
| 20 | + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
| 21 | + this.readLock = lock.readLock(); |
| 22 | + this.writeLock = lock.writeLock(); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public boolean put(K key, V value) { |
| 27 | + writeLock.lock(); |
| 28 | + try { |
| 29 | + CacheElement<K, V> item = new CacheElement<K, V>(key, value); |
| 30 | + LinkedListNode<CacheElement<K, V>> newNode; |
| 31 | + if (this.linkedListNodeMap.containsKey(key)) { |
| 32 | + LinkedListNode<CacheElement<K, V>> node = this.linkedListNodeMap.get(key); |
| 33 | + newNode = doublyLinkedList.updateAndMoveToFront(node, item); |
| 34 | + } else { |
| 35 | + if (this.size() >= this.size) { |
| 36 | + this.evictElement(); |
| 37 | + } |
| 38 | + newNode = this.doublyLinkedList.add(item); |
| 39 | + } |
| 40 | + if (newNode.isEmpty()) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + this.linkedListNodeMap.put(key, newNode); |
| 44 | + return true; |
| 45 | + } finally { |
| 46 | + writeLock.unlock(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public Optional<V> get(K key) { |
| 52 | + readLock.lock(); |
| 53 | + try { |
| 54 | + LinkedListNode<CacheElement<K, V>> linkedListNode = this.linkedListNodeMap.get(key); |
| 55 | + if (linkedListNode != null && !linkedListNode.isEmpty()) { |
| 56 | + linkedListNodeMap.put(key, this.doublyLinkedList.moveToFront(linkedListNode)); |
| 57 | + return Optional.of(linkedListNode.getElement().getValue()); |
| 58 | + } |
| 59 | + return Optional.empty(); |
| 60 | + } finally { |
| 61 | + readLock.unlock(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public int size() { |
| 67 | + readLock.lock(); |
| 68 | + try { |
| 69 | + return doublyLinkedList.size(); |
| 70 | + } finally { |
| 71 | + readLock.unlock(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean isEmpty() { |
| 77 | + return size() == 0; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void clear() { |
| 82 | + writeLock.lock(); |
| 83 | + try { |
| 84 | + linkedListNodeMap.clear(); |
| 85 | + doublyLinkedList.clear(); |
| 86 | + } finally { |
| 87 | + writeLock.unlock(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + private boolean evictElement() { |
| 93 | + writeLock.lock(); |
| 94 | + try { |
| 95 | + LinkedListNode<CacheElement<K, V>> linkedListNode = doublyLinkedList.removeTail(); |
| 96 | + if (linkedListNode.isEmpty()) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + linkedListNodeMap.remove(linkedListNode.getElement().getKey()); |
| 100 | + return true; |
| 101 | + } finally { |
| 102 | + writeLock.unlock(); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments