* BAEL-4464 : how to implement LRU-Cache in java codes added

* BAEL-4464 : how to implement LRU-Cache in java codes added - package named fixed

* BAEL-4464 : how to implement LRU-Cache in java codes added - package named changed

* BAEL-4464 : how to implement LRU-Cache in java codes added - unitTest fixed

* BAEL-4464 : issues 4,5 fixed.

* fixed some issues in BAEL-4464
This commit is contained in:
Arash Ariani 2021-07-20 09:51:36 +04:30 committed by GitHub
parent b9571f02f5
commit 66b40ed662
2 changed files with 34 additions and 44 deletions

View File

@ -10,67 +10,62 @@ public class DoublyLinkedList<T> {
private LinkedListNode<T> head;
private LinkedListNode<T> tail;
private AtomicInteger size;
private ReentrantReadWriteLock.ReadLock readLock;
private ReentrantReadWriteLock.WriteLock writeLock;
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public DoublyLinkedList() {
this.dummyNode = new DummyNode<T>(this);
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
readLock = lock.readLock();
writeLock = lock.writeLock();
clear();
}
public void clear() {
writeLock.lock();
this.lock.writeLock().lock();
try {
head = dummyNode;
tail = dummyNode;
size = new AtomicInteger(0);
} finally {
writeLock.unlock();
this.lock.writeLock().unlock();
}
}
public int size() {
readLock.lock();
this.lock.readLock().lock();
try {
return size.get();
} finally {
readLock.unlock();
this.lock.readLock().unlock();
}
}
public boolean isEmpty() {
readLock.lock();
this.lock.readLock().lock();
try {
return head.isEmpty();
} finally {
readLock.unlock();
this.lock.readLock().unlock();
}
}
public boolean contains(T value) {
readLock.lock();
this.lock.readLock().lock();
try {
return search(value).hasElement();
} finally {
readLock.unlock();
this.lock.readLock().unlock();
}
}
public LinkedListNode<T> search(T value) {
readLock.lock();
this.lock.readLock().lock();
try {
return head.search(value);
} finally {
readLock.unlock();
this.lock.readLock().unlock();
}
}
public LinkedListNode<T> add(T value) {
writeLock.lock();
this.lock.writeLock().lock();
try {
head = new Node<T>(value, head, this);
if (tail.isEmpty()) {
@ -79,12 +74,12 @@ public class DoublyLinkedList<T> {
size.incrementAndGet();
return head;
} finally {
writeLock.unlock();
this.lock.writeLock().unlock();
}
}
public boolean addAll(Collection<T> values) {
writeLock.lock();
this.lock.writeLock().lock();
try {
for (T value : values) {
if (add(value).isEmpty()) {
@ -93,12 +88,12 @@ public class DoublyLinkedList<T> {
}
return true;
} finally {
writeLock.unlock();
this.lock.writeLock().unlock();
}
}
public LinkedListNode<T> remove(T value) {
writeLock.lock();
this.lock.writeLock().lock();
try {
LinkedListNode<T> linkedListNode = head.search(value);
if (!linkedListNode.isEmpty()) {
@ -113,12 +108,12 @@ public class DoublyLinkedList<T> {
}
return linkedListNode;
} finally {
writeLock.unlock();
this.lock.writeLock().unlock();
}
}
public LinkedListNode<T> removeTail() {
writeLock.lock();
this.lock.writeLock().lock();
try {
LinkedListNode<T> oldTail = tail;
if (oldTail == head) {
@ -132,7 +127,7 @@ public class DoublyLinkedList<T> {
}
return oldTail;
} finally {
writeLock.unlock();
this.lock.writeLock().unlock();
}
}
@ -141,7 +136,7 @@ public class DoublyLinkedList<T> {
}
public LinkedListNode<T> updateAndMoveToFront(LinkedListNode<T> node, T newValue) {
writeLock.lock();
this.lock.writeLock().lock();
try {
if (node.isEmpty() || (this != (node.getListReference()))) {
return dummyNode;
@ -150,7 +145,7 @@ public class DoublyLinkedList<T> {
add(newValue);
return head;
} finally {
writeLock.unlock();
this.lock.writeLock().unlock();
}
}

View File

@ -1,6 +1,5 @@
package com.baeldung.lrucache;
import java.util.Hashtable;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
@ -10,21 +9,17 @@ public class LRUCache<K, V> implements Cache<K, V> {
private int size;
private Map<K, LinkedListNode<CacheElement<K, V>>> linkedListNodeMap;
private DoublyLinkedList<CacheElement<K, V>> doublyLinkedList;
private ReentrantReadWriteLock.ReadLock readLock;
private ReentrantReadWriteLock.WriteLock writeLock;
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public LRUCache(int size) {
this.size = size;
this.linkedListNodeMap = new Hashtable<>(size);
this.linkedListNodeMap = new ConcurrentHashMap<>(size);
this.doublyLinkedList = new DoublyLinkedList<>();
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
this.readLock = lock.readLock();
this.writeLock = lock.writeLock();
}
@Override
public boolean put(K key, V value) {
writeLock.lock();
this.lock.writeLock().lock();
try {
CacheElement<K, V> item = new CacheElement<K, V>(key, value);
LinkedListNode<CacheElement<K, V>> newNode;
@ -43,13 +38,13 @@ public class LRUCache<K, V> implements Cache<K, V> {
this.linkedListNodeMap.put(key, newNode);
return true;
} finally {
writeLock.unlock();
this.lock.writeLock().unlock();
}
}
@Override
public Optional<V> get(K key) {
readLock.lock();
this.lock.readLock().lock();
try {
LinkedListNode<CacheElement<K, V>> linkedListNode = this.linkedListNodeMap.get(key);
if (linkedListNode != null && !linkedListNode.isEmpty()) {
@ -58,17 +53,17 @@ public class LRUCache<K, V> implements Cache<K, V> {
}
return Optional.empty();
} finally {
readLock.unlock();
this.lock.readLock().unlock();
}
}
@Override
public int size() {
readLock.lock();
this.lock.readLock().lock();
try {
return doublyLinkedList.size();
} finally {
readLock.unlock();
this.lock.readLock().unlock();
}
}
@ -79,18 +74,18 @@ public class LRUCache<K, V> implements Cache<K, V> {
@Override
public void clear() {
writeLock.lock();
this.lock.writeLock().lock();
try {
linkedListNodeMap.clear();
doublyLinkedList.clear();
} finally {
writeLock.unlock();
this.lock.writeLock().unlock();
}
}
private boolean evictElement() {
writeLock.lock();
this.lock.writeLock().lock();
try {
LinkedListNode<CacheElement<K, V>> linkedListNode = doublyLinkedList.removeTail();
if (linkedListNode.isEmpty()) {
@ -99,7 +94,7 @@ public class LRUCache<K, V> implements Cache<K, V> {
linkedListNodeMap.remove(linkedListNode.getElement().getKey());
return true;
} finally {
writeLock.unlock();
this.lock.writeLock().unlock();
}
}
}