Lightweight Java Object cache API [closed]
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.
CacheManager cacheManager = CacheManager.getInstance();int oneDay = 24 * 60 * 60;Cache memoryOnlyCache = new Cache("name", 200, false, false, oneDay, oneDay);cacheManager.addCache(memoryOnlyCache);
Creates a cache that will hold 200 elements, and has a ttl of 24 hours.
I really like the MapMaker
that comes with Google Guava (API)
The JavaDoc has a pretty neat example that demonstrates both its ease of use and its power:
ConcurrentMap<Key, Graph> graphs = new MapMaker() .concurrencyLevel(32) .softKeys() .weakValues() .expiration(30, TimeUnit.MINUTES) .makeComputingMap( new Function<Key, Graph>() { public Graph apply(Key key) { return createExpensiveGraph(key); } });
Furthermore, release 10.0 of Guava introduced the much more extensive com.google.common.cache
package (there's a nice wiki entry on how to use them).
You can also check out my little cache library called KittyCache at:
https://github.com/treeder/kitty-cache
There are some performance benchmarks vs ehcache.
It's used in the SimpleJPA project as a second level cache.