Added fix for another potential multithreaded case for cache.

Case: consecutive gets.
This commit is contained in:
Harrison Deng 2020-04-30 01:03:52 -05:00
parent 83297a1d10
commit c60e94728e
2 changed files with 3 additions and 0 deletions

View File

@ -31,6 +31,7 @@ public class Cache<K, V> {
} else { } else {
lock.lock(); lock.lock();
try { try {
if (data.containsKey(key)) return;
CacheValue<K, V> val = new CacheValue<>(); CacheValue<K, V> val = new CacheValue<>();
val.key = key; val.key = key;
val.value = value; val.value = value;

View File

@ -8,6 +8,7 @@ class CacheUsageStack<K, V> {
private final ReentrantLock lock = new ReentrantLock(); private final ReentrantLock lock = new ReentrantLock();
private void addValueToStackTop(CacheValue<K, V> value) { private void addValueToStackTop(CacheValue<K, V> value) {
if (value == first) return;
if (first != null) { if (first != null) {
first.front = value; first.front = value;
value.back = first; value.back = first;
@ -91,6 +92,7 @@ class CacheUsageStack<K, V> {
} finally { } finally {
lock.unlock(); lock.unlock();
} }
size = 0;
} }
public int size() { public int size() {