diff --git a/java/com/google/gerrit/server/cache/CacheBinding.java b/java/com/google/gerrit/server/cache/CacheBinding.java index abb0f326ed..ff164ba723 100644 --- a/java/com/google/gerrit/server/cache/CacheBinding.java +++ b/java/com/google/gerrit/server/cache/CacheBinding.java @@ -37,8 +37,30 @@ public interface CacheBinding { /** Algorithm to weigh an object with a method other than the unit weight 1. */ CacheBinding weigher(Class> clazz); + /** + * Set the config name to something other than the cache name. + * + * @see #configKey() + */ + CacheBinding configKey(String configKey); + + /** + * Unique name for this cache. + * + *

The name can be used in a binding annotation {@code @Named(name)} to inject the cache + * configured with this binding. + */ String name(); + /** + * Key to use when looking up configuration for this cache. + * + *

Typically, this will match the result of {@link #name()}, so that configuration is keyed by + * the actual cache name. However, it may be changed, for example to reuse the size limits of some + * other cache. + */ + String configKey(); + TypeLiteral keyType(); TypeLiteral valueType(); diff --git a/java/com/google/gerrit/server/cache/CacheProvider.java b/java/com/google/gerrit/server/cache/CacheProvider.java index 86df10415d..b1a72ca8a8 100644 --- a/java/com/google/gerrit/server/cache/CacheProvider.java +++ b/java/com/google/gerrit/server/cache/CacheProvider.java @@ -33,6 +33,7 @@ class CacheProvider implements Provider>, CacheBinding { final String name; private final TypeLiteral keyType; private final TypeLiteral valType; + private String configKey; private boolean persist; private long maximumWeight; private long diskLimit; @@ -109,6 +110,13 @@ class CacheProvider implements Provider>, CacheBinding { return this; } + @Override + public CacheBinding configKey(String name) { + Preconditions.checkState(!frozen, "binding frozen, cannot be modified"); + configKey = Preconditions.checkNotNull(name); + return this; + } + @Override public String name() { if (!Strings.isNullOrEmpty(plugin)) { @@ -117,6 +125,11 @@ class CacheProvider implements Provider>, CacheBinding { return name; } + @Override + public String configKey() { + return configKey != null ? configKey : name(); + } + @Override public TypeLiteral keyType() { return keyType; diff --git a/java/com/google/gerrit/server/cache/h2/H2CacheBindingProxy.java b/java/com/google/gerrit/server/cache/h2/H2CacheBindingProxy.java index 0d1cf20bf3..2871080466 100644 --- a/java/com/google/gerrit/server/cache/h2/H2CacheBindingProxy.java +++ b/java/com/google/gerrit/server/cache/h2/H2CacheBindingProxy.java @@ -60,6 +60,11 @@ class H2CacheBindingProxy implements CacheBinding { return source.name(); } + @Override + public String configKey() { + return source.configKey(); + } + @Override public TypeLiteral keyType() { return source.keyType(); @@ -85,6 +90,11 @@ class H2CacheBindingProxy implements CacheBinding { return source.loader(); } + @Override + public CacheBinding configKey(String configKey) { + throw new RuntimeException(MSG_NOT_SUPPORTED); + } + @Override public CacheBinding maximumWeight(long weight) { throw new RuntimeException(MSG_NOT_SUPPORTED); diff --git a/java/com/google/gerrit/server/cache/h2/H2CacheFactory.java b/java/com/google/gerrit/server/cache/h2/H2CacheFactory.java index 2240c7d4a1..db964c35ee 100644 --- a/java/com/google/gerrit/server/cache/h2/H2CacheFactory.java +++ b/java/com/google/gerrit/server/cache/h2/H2CacheFactory.java @@ -155,7 +155,7 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener { @SuppressWarnings({"unchecked"}) @Override public Cache build(CacheBinding in) { - long limit = config.getLong("cache", in.name(), "diskLimit", in.diskLimit()); + long limit = config.getLong("cache", in.configKey(), "diskLimit", in.diskLimit()); if (cacheDir == null || limit <= 0) { return memCacheFactory.build(in); @@ -176,7 +176,7 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener { @SuppressWarnings("unchecked") @Override public LoadingCache build(CacheBinding in, CacheLoader loader) { - long limit = config.getLong("cache", in.name(), "diskLimit", in.diskLimit()); + long limit = config.getLong("cache", in.configKey(), "diskLimit", in.diskLimit()); if (cacheDir == null || limit <= 0) { return memCacheFactory.build(in, loader); diff --git a/java/com/google/gerrit/server/cache/mem/DefaultMemoryCacheFactory.java b/java/com/google/gerrit/server/cache/mem/DefaultMemoryCacheFactory.java index 114e893c03..126da8ebc9 100644 --- a/java/com/google/gerrit/server/cache/mem/DefaultMemoryCacheFactory.java +++ b/java/com/google/gerrit/server/cache/mem/DefaultMemoryCacheFactory.java @@ -55,7 +55,8 @@ class DefaultMemoryCacheFactory implements MemoryCacheFactory { private CacheBuilder create(CacheBinding def) { CacheBuilder builder = newCacheBuilder(); builder.recordStats(); - builder.maximumWeight(cfg.getLong("cache", def.name(), "memoryLimit", def.maximumWeight())); + builder.maximumWeight( + cfg.getLong("cache", def.configKey(), "memoryLimit", def.maximumWeight())); builder = builder.removalListener(forwardingRemovalListenerFactory.create(def.name())); @@ -66,10 +67,10 @@ class DefaultMemoryCacheFactory implements MemoryCacheFactory { builder.weigher(weigher); Long age = def.expireAfterWrite(TimeUnit.SECONDS); - if (has(def.name(), "maxAge")) { + if (has(def.configKey(), "maxAge")) { builder.expireAfterWrite( ConfigUtil.getTimeUnit( - cfg, "cache", def.name(), "maxAge", age != null ? age : 0, TimeUnit.SECONDS), + cfg, "cache", def.configKey(), "maxAge", age != null ? age : 0, TimeUnit.SECONDS), TimeUnit.SECONDS); } else if (age != null) { builder.expireAfterWrite(age, TimeUnit.SECONDS);