Merge "Allow binding the module without GitRepositoryManager" into stable-3.3
diff --git a/README.md b/README.md
index bdcc00d..1643f01 100644
--- a/README.md
+++ b/README.md
@@ -76,6 +76,19 @@
   com.googlesource.gerrit.plugins.cachedrefdb.LibSysModule
 ```
 
+> NOTE: There are situations where the binding of the module to the Gerrit's
+> GitRepositoryManager is not desired; e.g., when using this module together
+> with others that are trying to override it at the same time.
+>
+> It is possible to just load the module using the following two options:
+>
+> ```
+> git config --file ${GERRIT_SITE}/etc/gerrit.config --add gerrit.installDbModule\
+>   com.googlesource.gerrit.plugins.cachedrefdb.LibModule
+> git config --file ${GERRIT_SITE}/etc/gerrit.config --add gerrit.installModule\
+>   com.googlesource.gerrit.plugins.cachedrefdb.LibSysModule
+> ```
+
 By default cache can hold up to `1024` refs which will not be sufficient for
 any production site therefore one can configure it through the standard Gerrit
 cache configuration means e.g.
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/LibDbModule.java b/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/LibDbModule.java
index 109126d..2bbf063 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/LibDbModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/LibDbModule.java
@@ -14,10 +14,7 @@
 
 package com.googlesource.gerrit.plugins.cachedrefdb;
 
-import static com.google.inject.Scopes.SINGLETON;
-
 import com.google.common.flogger.FluentLogger;
-import com.google.gerrit.extensions.registration.DynamicItem;
 import com.google.gerrit.lifecycle.LifecycleModule;
 import com.google.gerrit.server.ModuleImpl;
 import com.google.gerrit.server.config.RepositoryConfig;
@@ -40,14 +37,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);
-    factory(BatchRefUpdateWithCacheUpdate.Factory.class);
-    factory(CachedRefDatabase.Factory.class);
-    factory(CachedRefRepository.Factory.class);
+    install(new LibModule());
 
     bind(GitRepositoryManager.class).to(CachedGitRepositoryManager.class);
 
@@ -56,6 +46,6 @@
     if (!repoConfig.getAllBasePaths().isEmpty()) {
       bind(LocalDiskRepositoryManager.class).to(MultiBaseLocalDiskRepositoryManager.class);
     }
-    logger.atInfo().log("DB library loaded");
+    logger.atInfo().log("GitRepositoryManager bind completed");
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/LibModule.java b/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/LibModule.java
new file mode 100644
index 0000000..8e9eb71
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/cachedrefdb/LibModule.java
@@ -0,0 +1,39 @@
+// Copyright (C) 2022 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.cachedrefdb;
+
+import static com.google.inject.Scopes.SINGLETON;
+
+import com.google.common.flogger.FluentLogger;
+import com.google.gerrit.extensions.registration.DynamicItem;
+import com.google.gerrit.lifecycle.LifecycleModule;
+
+public class LibModule extends LifecycleModule {
+  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
+
+  @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);
+    factory(BatchRefUpdateWithCacheUpdate.Factory.class);
+    factory(CachedRefDatabase.Factory.class);
+    factory(CachedRefRepository.Factory.class);
+
+    logger.atInfo().log("Ref repository and db library loaded");
+  }
+}