-
-
Save dakshj/7681266e2dba6f098d75eef06c220fa8 to your computer and use it in GitHub Desktop.
A passive TTL hash map, that expires and removes values if they are older than some time-to-live. I have only proved this correct, not tried it ;)
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.example; | |
import static java.util.Collections.*; | |
import java.util.Collection; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* A hash map that expires and removes items if they are older than a given | |
* time-to-live. | |
* <p> | |
* The expiry is a passive process, items aren't removed until they are | |
* retrieved and deemed to be expired by {@link #get(Object)}. | |
*/ | |
public class TtlHashMap<K, V> implements Map<K, V> { | |
private final HashMap<K, V> store = new HashMap<>(); | |
private final HashMap<K, Long> timestamps = new HashMap<>(); | |
private final long ttl; | |
public TtlHashMap(TimeUnit ttlUnit, long ttlValue) { | |
this.ttl = ttlUnit.toNanos(ttlValue); | |
} | |
@Override | |
public V get(Object key) { | |
V value = this.store.get(key); | |
if (value != null && expired(key, value)) { | |
store.remove(key); | |
timestamps.remove(key); | |
return null; | |
} else { | |
return value; | |
} | |
} | |
private boolean expired(Object key, V value) { | |
return (System.nanoTime() - timestamps.get(key)) > this.ttl; | |
} | |
@Override | |
public V put(K key, V value) { | |
timestamps.put(key, System.nanoTime()); | |
return store.put(key, value); | |
} | |
@Override | |
public int size() { | |
clearExpired(); | |
return store.size(); | |
} | |
@Override | |
public boolean isEmpty() { | |
return store.isEmpty(); | |
} | |
@Override | |
public boolean containsKey(Object key) { | |
return store.containsKey(key); | |
} | |
@Override | |
public boolean containsValue(Object value) { | |
return store.containsValue(value); | |
} | |
@Override | |
public V remove(Object key) { | |
timestamps.remove(key); | |
return store.remove(key); | |
} | |
@Override | |
public void putAll(Map<? extends K, ? extends V> m) { | |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) { | |
this.put(e.getKey(), e.getValue()); | |
} | |
} | |
@Override | |
public void clear() { | |
timestamps.clear(); | |
store.clear(); | |
} | |
@Override | |
public Set<K> keySet() { | |
clearExpired(); | |
return unmodifiableSet(store.keySet()); | |
} | |
@Override | |
public Collection<V> values() { | |
clearExpired(); | |
return unmodifiableCollection(store.values()); | |
} | |
@Override | |
public Set<java.util.Map.Entry<K, V>> entrySet() { | |
clearExpired(); | |
return unmodifiableSet(store.entrySet()); | |
} | |
private void clearExpired() { | |
for (K k : store.keySet()) { | |
this.get(k); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment