Skip to content

Instantly share code, notes, and snippets.

@shadowfacts shadowfacts/ExpiringMap.java Secret
Created May 13, 2019

Embed
What would you like to do?
class ExpiringMap<K, V> extends HashMap<K, Entry<V>> {
class Entry<V> {
V value;
long expiry;
}
long decayTime = 60_000;
V get(K key) {
Entry<V> e = super.get(key);
if (System.currentTimeMillis() >= expiry) {
remove(key);
return null;
}
return e.value;
}
void put(K key, V value) {
super.put(key, new Entry<>(value, System.currentTimeMillis() + decayTime));
}
void refresh(K key) {
Entry<V> e = super.get(key);
e.expiry = System.currentTimeMillis() + decayTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.