in evcache-core/src/main/java/com/netflix/evcache/EVCacheInMemoryCache.java [107:171]
private void setupCache() {
try {
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().recordStats();
if(_cacheSize.get() > 0) {
builder = builder.maximumSize(_cacheSize.get());
}
if(_exireAfterAccessDuration.get() > 0) {
builder = builder.expireAfterAccess(_exireAfterAccessDuration.get(), TimeUnit.MILLISECONDS);
} else if(_cacheDuration.get().intValue() > 0) {
builder = builder.expireAfterWrite(_cacheDuration.get(), TimeUnit.MILLISECONDS);
}
if(_refreshDuration.get() > 0) {
builder = builder.refreshAfterWrite(_refreshDuration.get(), TimeUnit.MILLISECONDS);
}
initRefreshPool();
final LoadingCache<EVCacheKey, Optional<T>> newCache = builder.build(
new CacheLoader<EVCacheKey, Optional<T>>() {
public Optional<T> load(EVCacheKey key) throws EVCacheException, DataNotFoundException {
try {
return Optional.fromNullable(impl.doGet(key, tc));
} catch (EVCacheException e) {
log.error("EVCacheException while loading key -> "+ key, e);
throw e;
} catch (Exception e) {
log.error("EVCacheException while loading key -> "+ key, e);
throw new EVCacheException("key : " + key + " could not be loaded", e);
}
}
@Override
public ListenableFuture<Optional<T>> reload(EVCacheKey key, Optional<T> oldValue) {
ListenableFutureTask<Optional<T>> task = ListenableFutureTask.create(new Callable<Optional<T>>() {
public Optional<T> call() {
try {
final Optional<T> t = load(key);
if(t == null) {
EVCacheMetricsFactory.getInstance().increment("EVCacheInMemoryCache" + "-" + appName + "-Reload-NotFound");
return oldValue;
} else {
EVCacheMetricsFactory.getInstance().increment("EVCacheInMemoryCache" + "-" + appName + "-Reload-Success");
}
return t;
} catch (EVCacheException e) {
log.error("EVCacheException while reloading key -> "+ key, e);
EVCacheMetricsFactory.getInstance().increment("EVCacheInMemoryCache" + "-" + appName + "-Reload-Fail");
return oldValue;
}
}
});
pool.execute(task);
return task;
}
});
if(cache != null) newCache.putAll(cache.asMap());
final Cache<EVCacheKey, Optional<T>> currentCache = this.cache;
this.cache = newCache;
if(currentCache != null) {
currentCache.invalidateAll();
currentCache.cleanUp();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}