Made sure occurrences are properly tracked.

This commit is contained in:
Harrison Deng 2020-04-25 18:16:18 -05:00
parent 4b6cfacc47
commit dcec9fd7e7

View File

@ -26,7 +26,7 @@ public class Cache<Key, Value> {
while (occurrences.get(occurrenceOrder.peek()) > 1) { while (occurrences.get(occurrenceOrder.peek()) > 1) {
occurencesTrackedPoll(); occurencesTrackedPoll();
} }
data.remove(occurrenceOrder.poll()); data.remove(occurencesTrackedPoll());
} }
occurrencesTrackedAdd(key); occurrencesTrackedAdd(key);
} }
@ -63,12 +63,12 @@ public class Cache<Key, Value> {
* @param key the key associated with the value. * @param key the key associated with the value.
* @return the value associated to the key. * @return the value associated to the key.
*/ */
public Value getValue(Key key) { public synchronized Value getValue(Key key) {
occurrencesTrackedAdd(key); occurrencesTrackedAdd(key);
return data.get(key); return data.get(key);
} }
public void clearCache() { public synchronized void clearCache() {
data.clear(); data.clear();
occurrenceOrder.clear(); occurrenceOrder.clear();
occurrences.clear(); occurrences.clear();