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 {
lock.lock();
try {
if (data.containsKey(key)) return;
CacheValue<K, V> val = new CacheValue<>();
val.key = key;
val.value = value;

View File

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