Changed locking positions.

This commit is contained in:
Harrison Deng 2020-04-29 13:05:04 -05:00
parent d62bc696a5
commit a102f53365

View File

@ -30,11 +30,11 @@ public class Cache<K, V> {
* @param value The value to store. * @param value The value to store.
*/ */
public void set(K key, V value) { public void set(K key, V value) {
writeLock.lock();
try {
if (data.containsKey(key)) { if (data.containsKey(key)) {
data.get(key).value = value; data.get(key).value = value;
} else { } else {
writeLock.lock();
try {
CacheValue<K, V> val = new CacheValue<>(); CacheValue<K, V> val = new CacheValue<>();
val.key = key; val.key = key;
val.value = value; val.value = value;
@ -43,11 +43,11 @@ public class Cache<K, V> {
if (data.size() > maxSize) { if (data.size() > maxSize) {
data.remove(usage.pop().key); data.remove(usage.pop().key);
} }
}
} finally { } finally {
writeLock.unlock(); writeLock.unlock();
} }
} }
}
/** /**
* Retrieves cached value associated to the key. * Retrieves cached value associated to the key.