Avoid writing a cache with an outdated counter from the start
@@ -116,11 +116,12 @@ public class CuratorDatabase {
/** Invalidates the current cache if outdated. */
private CuratorDatabaseCache getCache() {
- long generation = changeGenerationCounter.get();
+ if (changeGenerationCounter.get() != cache.get().generation)
synchronized (cacheCreationLock) {
- if (generation != cache.get().generation)
- cache.set(newCache(generation));
+ while (changeGenerationCounter.get() != cache.get().generation)
+ cache.set(newCache(changeGenerationCounter.get()));
}
+
return cache.get();
}
