As I have a bulk operation for obtaining data from, so rather than using refresh() to update an individual item, I want to take advantage of bulk call to periodically to update the cache.
I have some approaches in mind, as I have get() call to cache all the time, I wonder which approach is the optimal.
Approach 1 Periodically call getAll(), which calls loadAll()
LoadingCache<String, String> cache = CacheBuilder.newBuilder()
.ticker(ticker)
.refreshAfterWrite(5, TimeUnit.Minut)
.expireAfterAccess(30, TimeUnit.MINUTES)
.build(cacheLoader);
private static final class CacheLoader extends CacheLoader<String, String> {
@Override
public String load(final String key) {
return db.getItem(key);
}
@Override
public ListenableFuture<String> reload(String key) {
ListenableFutureTask<String> task = ListenableFutureTask.create(new Callable<String>() {
public String call() {
return db.getItem(key);
}
});
executor.execute(task);
return task;
}
@Override
public String loadAll(final List<String> key) {
return db.bulkGetItems(keyList);
}
}
private class RefresherRunnable implements Runnable {
private volatile boolean stopped = false;
@Override
public void run() {
while (!stopped) {
try {
List<String> keys = getKeys();
cache.getAll(keys);
} catch (Exception e) {
//log
}
final long nextSleepInterval = 1000 * 60;
try {
Thread.sleep(nextSleepInterval);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
Approach 2
Just use Cache.asMap().putAll
private class RefresherRunnable implements Runnable {
private volatile boolean stopped = false;
@Override
public void run() {
while (!stopped) {
try {
List<String> keys = getKeys();
Map<String, String> keyValueMap = db.bulkGetItems(keys);
cache.asMap().putAll(keyValueMap);
} catch (Exception e) {
//log
}
final long nextSleepInterval = 1000 * 60;
try {
Thread.sleep(nextSleepInterval);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
I read about from https://github.com/google/guava/wiki/CachesExplained#inserted-directly it sounds like getAll is better than asMap.put(), so approach 1 is preferred, I guess?