Do any of you know of a Java Map or similar standard data store that automatically purges entries after a given timeout? This means aging, where the old expired entries “age-out” automatically.
I know of ways to implement the functionality myself and have done it several times in the past, so I’m not asking for advice in that respect, but for pointers to a good reference implementation.
WeakReference based solutions like WeakHashMap are not an option, because my keys are likely to be non-interned strings and I want a configurable timeout that’s not dependent on the garbage collector.
Ehcache is also an option I wouldn’t like to rely on because it needs external configuration files. I am looking for a code-only solution.
Yes. Google Collections, or Guava as it is named now has something called MapMaker which can do exactly that.
Update:
As of guava 10.0 (released September 28, 2011) many of these MapMaker methods have been deprecated in favour of the new CacheBuilder: