Minor improvement to cache by reducing area under lock.

This commit is contained in:
Harrison Deng 2020-04-26 02:04:29 -05:00
parent 98e4265db7
commit 925296f326

View File

@ -31,11 +31,11 @@ public class Cache<Key, Value> {
if (data.size() > maxSize) {
int occ = 0;
do {
Key potentialKey = occurrenceOrder.poll();
CacheValue<Value> potential = data.get(potentialKey);
potential.occurrence--;
cleaningLock.lock();
try {
Key potentialKey = occurrenceOrder.poll();
CacheValue<Value> potential = data.get(potentialKey);
potential.occurrence--;
occ = potential.occurrence;
if (occ < 1) {
data.remove(potentialKey);