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) { if (data.size() > maxSize) {
int occ = 0; int occ = 0;
do { do {
Key potentialKey = occurrenceOrder.poll();
CacheValue<Value> potential = data.get(potentialKey);
potential.occurrence--;
cleaningLock.lock(); cleaningLock.lock();
try { try {
Key potentialKey = occurrenceOrder.poll();
CacheValue<Value> potential = data.get(potentialKey);
potential.occurrence--;
occ = potential.occurrence; occ = potential.occurrence;
if (occ < 1) { if (occ < 1) {
data.remove(potentialKey); data.remove(potentialKey);