Question
I’m looking for a Java in-memory object caching API. Any recommendations? What solutions have you used in the past?
Current
Right now, I’m just using a Map:
Map cache = new HashMap<String, Object>(); cache.put('key', value);
Requirements
I need to extend the cache to include basic features like:
- Max size
- Time to live
However, I don’t need more sophisticated features like:
- Access from multiple processes (caching server)
- Persistence (to disk)
Suggestions
In-Memory caching:
- Guava CacheBuilder – active development. See this presentation.
- LRUMap – Config via API. No TTL. Not purpose built for caching.
- whirlycache – XML config. Mailing list. Last updated 2006.
- cache4j – XML config. Documentation in Russian. Last updated 2006.
Enterprise caching:
EHCache is very nice. You can create an in memory cache. Check out their code samples for an example of creating an in memory cache. You can specify a max size, and a time to live.
EHCache does offer some advanced features, but if your not interested in using them – don’t. But it’s nice to know they are there if your requirements ever change.
Here is an in memory cache. Created in code, with no configuration files.
Creates a cache that will hold 200 elements, and has a ttl of 24 hours.