Set RefByNameGerritCache to DynamicItem on Gerrit start

Change-Id: I908f7a1484feef2a871612c067bf7c182bfa0b09
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/LibDbModule.java b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/LibDbModule.java
index 5787abe..3c857cf 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/LibDbModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/LibDbModule.java
@@ -14,6 +14,8 @@
 
 package com.googlesource.gerrit.plugins.gerritcachedrefdb;
 
+import static com.google.inject.Scopes.SINGLETON;
+
 import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.extensions.config.FactoryModule;
 import com.google.gerrit.extensions.registration.DynamicItem;
@@ -25,6 +27,7 @@
   @Override
   protected void configure() {
     DynamicItem.itemOf(binder(), RefByNameCache.class);
+    DynamicItem.bind(binder(), RefByNameCache.class).to(NoOpRefByNameCache.class).in(SINGLETON);
 
     factory(RefUpdateWithCacheUpdate.Factory.class);
     factory(RefRenameWithCacheUpdate.Factory.class);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/LibSysModule.java b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/LibSysModule.java
index e6dab8f..7e93baf 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/LibSysModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gerritcachedrefdb/LibSysModule.java
@@ -14,19 +14,49 @@
 
 package com.googlesource.gerrit.plugins.gerritcachedrefdb;
 
-import static com.google.inject.Scopes.SINGLETON;
-
 import com.google.common.flogger.FluentLogger;
+import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.extensions.registration.DynamicItem;
-import com.google.inject.AbstractModule;
+import com.google.gerrit.extensions.registration.RegistrationHandle;
+import com.google.gerrit.lifecycle.LifecycleModule;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
 
-public class LibSysModule extends AbstractModule {
+public class LibSysModule extends LifecycleModule {
   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
 
   @Override
   protected void configure() {
     install(RefByNameGerritCache.module());
-    DynamicItem.bind(binder(), RefByNameCache.class).to(RefByNameGerritCache.class).in(SINGLETON);
+    listener().to(RefByNameGerritCacheSetter.class);
     logger.atInfo().log("Sys library loaded");
   }
+
+  @Singleton
+  private static class RefByNameGerritCacheSetter implements LifecycleListener {
+    private final RefByNameGerritCache refByNameGerritCache;
+    private final DynamicItem<RefByNameCache> cacheRef;
+    private RegistrationHandle handle;
+
+    @Inject
+    RefByNameGerritCacheSetter(
+        RefByNameGerritCache refByNameGerritCache, DynamicItem<RefByNameCache> cacheRef) {
+      this.refByNameGerritCache = refByNameGerritCache;
+      this.cacheRef = cacheRef;
+    }
+
+    @Override
+    public void start() {
+      handle = cacheRef.set(refByNameGerritCache, "gerrit");
+      logger.atInfo().log("Gerrit-cache-backed RefDB loaded");
+    }
+
+    @Override
+    public void stop() {
+      if (handle != null) {
+        handle.remove();
+        logger.atInfo().log("Gerrit-cache-backed RefDB unloaded");
+      }
+    }
+  }
 }