Simplify RefByNameCacheWrapper There is no need to excercise DynamicItem for content for each cache operation as it cannot contain 'null' as its reference is set to 'NoOpRefByNameCache' when LibDbModule is loaded. All classes that refer to 'RefByNameCacheWrapper' are loaded in this module too. In case when 'LibSysModule' is not loaded (due to missing entry in gerrit.config) then it doesn't matter as cache will not be created anyway). In case when 'LibSysModule' is loaded then during the Gerrit start DynamicItem value will be set to 'RefByNameGerritCache' instance (which is a part of the same Module). Note that RefByNameCacheWrapper is not a singleton therefore it will evaluate DynamicItem once it gets crated together with 'CachedRefDatabase' instance each time when it is requested. Change-Id: I80bc9ac417c8ac3ee6e5c6e1ae8637ed0e4023d3
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefByNameCacheWrapper.java b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefByNameCacheWrapper.java index c9744ad..9ce127a 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefByNameCacheWrapper.java +++ b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/RefByNameCacheWrapper.java
@@ -24,22 +24,26 @@ class RefByNameCacheWrapper implements RefByNameCache { private static final RefByNameCache NOOP_CACHE = new NoOpRefByNameCache(); - @Inject(optional = true) - private DynamicItem<RefByNameCache> refByNameCache; + private final RefByNameCache cache; + + @Inject + RefByNameCacheWrapper(DynamicItem<RefByNameCache> refByNameCache) { + this.cache = Optional.ofNullable(refByNameCache.get()).orElse(NOOP_CACHE); + } @Override public Ref computeIfAbsent( String identifier, String ref, Callable<? extends Optional<Ref>> loader) { - return cache().computeIfAbsent(identifier, ref, loader); + return cache.computeIfAbsent(identifier, ref, loader); } @Override public void evict(String identifier, String ref) { - cache().evict(identifier, ref); + cache.evict(identifier, ref); } @VisibleForTesting RefByNameCache cache() { - return Optional.ofNullable(refByNameCache).map(DynamicItem::get).orElse(NOOP_CACHE); + return cache; } }