If you're ok with the cache in memory, I would suggest extending the Google Guava CacheLoader library, for example:
public class DBLoader extends CacheLoader<String, String> {
@Override
public String load(String key) throws Exception {
return value;
}
}
Then in use something like:
private LoadingCache<String, String> dbCache = CacheBuilder.newBuilder()
.expireAfterWrite(CACHE_EXPIRE_IN_SECONDS, TimeUnit.SECONDS)
.build(new DBLoader());
String value = dbCache.get(someKey);
Of course, you will need to carefully select the correct exception handling.
I find guava much easier than setting up ehcache properly.
source
share