Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Created January 25, 2019 16:14
Show Gist options
  • Save deque-blog/39f06552db9a95a731feaa1c190b57ee to your computer and use it in GitHub Desktop.
Save deque-blog/39f06552db9a95a731feaa1c190b57ee to your computer and use it in GitHub Desktop.
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