Last active
February 22, 2026 21:54
-
-
Save joelittlejohn/5565410 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 it correct, not tried it ;) [As the author of this code, I dedicate it to the public domain https://creativecommons.org/publicdomain/zero/1.0]
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.Iterator; | |
| 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)}. Certain operations | |
| * do cause the cache to clear all expired values in order to provide an accurate | |
| * result (keySet, entrySet, values, size) and these operations are therefore O(n). | |
| * <p/> | |
| * This class is not threadsafe. | |
| */ | |
| public class TtlHashMap<K, V> implements Map<K, V> { | |
| private final Map<K, V> store = new HashMap<>(); | |
| private final Map<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) { | |
| if (this.store.containsKey(key) && expired(key)) { | |
| store.remove(key); | |
| timestamps.remove(key); | |
| return null; | |
| } else { | |
| return this.store.get(key); | |
| } | |
| } | |
| private boolean expired(Object key) { | |
| 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() { | |
| clearExpired(); | |
| return store.isEmpty(); | |
| } | |
| @Override | |
| public boolean containsKey(Object key) { | |
| return store.containsKey(key) && !expired(key); | |
| } | |
| @Override | |
| public boolean containsValue(Object value) { | |
| clearExpired(); | |
| 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 (Iterator<Entry<K,V>> iter = store.entrySet().iterator(); iter.hasNext(); ) { | |
| Entry<K,V> entry = iter.next(); | |
| if (expired(entry.getKey())) { | |
| iter.remove(); | |
| timestamps.remove(entry.getKey()); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
😄 good jb