Skip to content

Instantly share code, notes, and snippets.

@sholokhov
Created July 29, 2016 21:37
Show Gist options
  • Save sholokhov/6ef89d80923b52167541feae7d4886f1 to your computer and use it in GitHub Desktop.
Save sholokhov/6ef89d80923b52167541feae7d4886f1 to your computer and use it in GitHub Desktop.
Lru simple cache
//
// simple least recently used cache
//
import java.util.LinkedHashMap;
import java.util.*;
class LruCache {
private LinkedHashMap cache = new LinkedHashMap<String, String>() {
private int maxSizeEntries = 10000;
@Override
protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
if (size() <= maxSizeEntries) {
return false;
}
Iterator<Map.Entry<String, String>> it = entrySet().iterator();
while (it.hasNext()) {
if (size() <= maxSizeEntries) {
return false;
}
it.next();
it.remove();
}
return false;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment