Created
July 29, 2016 21:37
-
-
Save sholokhov/6ef89d80923b52167541feae7d4886f1 to your computer and use it in GitHub Desktop.
Lru simple cache
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
// | |
// 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