Merge branch 'stable-3.3'

* stable-3.3:
  Use wrapper to store cache key

Change-Id: I46cd2d89b141eefa09b452e9c6229751d3775eab
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheFactory.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheFactory.java
index 491b5dc..b4cec3b 100644
--- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheFactory.java
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/ChronicleMapCacheFactory.java
@@ -16,15 +16,14 @@
 import com.google.common.cache.Cache;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
-import com.google.common.flogger.FluentLogger;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.extensions.registration.DynamicMap;
 import com.google.gerrit.metrics.MetricMaker;
 import com.google.gerrit.server.cache.CacheBackend;
 import com.google.gerrit.server.cache.MemoryCacheFactory;
+import com.google.gerrit.server.cache.PersistentCacheBaseFactory;
 import com.google.gerrit.server.cache.PersistentCacheDef;
-import com.google.gerrit.server.cache.PersistentCacheFactory;
 import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.gerrit.server.config.SitePaths;
 import com.google.gerrit.server.logging.LoggingContextAwareScheduledExecutorService;
@@ -34,7 +33,6 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.UncheckedIOException;
-import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.LinkedList;
 import java.util.List;
@@ -45,17 +43,12 @@
 import org.eclipse.jgit.lib.Config;
 
 @Singleton
-class ChronicleMapCacheFactory implements PersistentCacheFactory, LifecycleListener {
-  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
-
-  private final MemoryCacheFactory memCacheFactory;
-  private final Config config;
+class ChronicleMapCacheFactory extends PersistentCacheBaseFactory implements LifecycleListener {
   private final ChronicleMapCacheConfig.Factory configFactory;
   private final MetricMaker metricMaker;
   private final DynamicMap<Cache<?, ?>> cacheMap;
   private final List<ChronicleMapCacheImpl<?, ?>> caches;
   private final ScheduledExecutorService cleanup;
-  private final Path cacheDir;
 
   @Inject
   ChronicleMapCacheFactory(
@@ -65,12 +58,10 @@
       ChronicleMapCacheConfig.Factory configFactory,
       DynamicMap<Cache<?, ?>> cacheMap,
       MetricMaker metricMaker) {
-    this.memCacheFactory = memCacheFactory;
-    this.config = cfg;
+    super(memCacheFactory, cfg, site);
     this.configFactory = configFactory;
     this.metricMaker = metricMaker;
     this.caches = new LinkedList<>();
-    this.cacheDir = getCacheDir(site, cfg.getString("cache", null, "directory"));
     this.cacheMap = cacheMap;
     this.cleanup =
         new LoggingContextAwareScheduledExecutorService(
@@ -83,10 +74,8 @@
   }
 
   @Override
-  public <K, V> Cache<K, V> build(PersistentCacheDef<K, V> in, CacheBackend backend) {
-    if (isInMemoryCache(in)) {
-      return memCacheFactory.build(in, backend);
-    }
+  public <K, V> Cache<K, V> buildImpl(
+      PersistentCacheDef<K, V> in, long limit, CacheBackend backend) {
     ChronicleMapCacheConfig config =
         configFactory.create(
             in.configKey(),
@@ -106,11 +95,8 @@
   }
 
   @Override
-  public <K, V> LoadingCache<K, V> build(
-      PersistentCacheDef<K, V> in, CacheLoader<K, V> loader, CacheBackend backend) {
-    if (isInMemoryCache(in)) {
-      return memCacheFactory.build(in, loader, backend);
-    }
+  public <K, V> LoadingCache<K, V> buildImpl(
+      PersistentCacheDef<K, V> in, CacheLoader<K, V> loader, long limit, CacheBackend backend) {
     ChronicleMapCacheConfig config =
         configFactory.create(
             in.configKey(),
@@ -141,11 +127,6 @@
     }
   }
 
-  private <K, V> boolean isInMemoryCache(PersistentCacheDef<K, V> in) {
-    return cacheDir == null
-        || config.getLong("cache", in.configKey(), "diskLimit", in.diskLimit()) <= 0;
-  }
-
   @Override
   public void start() {
     for (ChronicleMapCacheImpl<?, ?> cache : caches) {
@@ -161,25 +142,4 @@
   public static File fileName(Path cacheDir, String name, Integer version) {
     return cacheDir.resolve(String.format("%s_%s.dat", name, version)).toFile();
   }
-
-  private static Path getCacheDir(SitePaths site, String name) {
-    if (name == null) {
-      return null;
-    }
-    Path loc = site.resolve(name);
-    if (!Files.exists(loc)) {
-      try {
-        Files.createDirectories(loc);
-      } catch (IOException e) {
-        logger.atWarning().log("Can't create disk cache: %s", loc.toAbsolutePath());
-        return null;
-      }
-    }
-    if (!Files.isWritable(loc)) {
-      logger.atWarning().log("Can't write to disk cache: %s", loc.toAbsolutePath());
-      return null;
-    }
-    logger.atInfo().log("Enabling disk cache %s", loc.toAbsolutePath());
-    return loc;
-  }
 }