Created
January 25, 2019 16:14
-
-
Save deque-blog/39f06552db9a95a731feaa1c190b57ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.murex.booking.sequence.sweeper.utils; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.function.Consumer; | |
public class LRUResourceCache<K, V> extends LinkedHashMap<K, V> { | |
private final int capacity; | |
private final Consumer<V> closeResource; | |
public LRUResourceCache(int capacity, Consumer<V> closeResource) { | |
super(capacity, 0.75f, true); | |
this.capacity = capacity; | |
this.closeResource = closeResource; | |
} | |
@Override | |
protected boolean removeEldestEntry(Map.Entry<K, V> oldest) { | |
if (size() > capacity) { | |
closeResource.accept(oldest.getValue()); | |
remove(oldest.getKey()); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment