Allow overriding the config key used to configure a cache
This will allow us to tie the size of one cache to match the size of another cache. Change-Id: I6e2fa78db8aec209fdc71ace74c28778c8192675
This commit is contained in:
@@ -37,8 +37,30 @@ public interface CacheBinding<K, V> {
|
||||
/** Algorithm to weigh an object with a method other than the unit weight 1. */
|
||||
CacheBinding<K, V> weigher(Class<? extends Weigher<K, V>> clazz);
|
||||
|
||||
/**
|
||||
* Set the config name to something other than the cache name.
|
||||
*
|
||||
* @see #configKey()
|
||||
*/
|
||||
CacheBinding<K, V> configKey(String configKey);
|
||||
|
||||
/**
|
||||
* Unique name for this cache.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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<K> keyType();
|
||||
|
||||
TypeLiteral<V> valueType();
|
||||
|
||||
@@ -33,6 +33,7 @@ class CacheProvider<K, V> implements Provider<Cache<K, V>>, CacheBinding<K, V> {
|
||||
final String name;
|
||||
private final TypeLiteral<K> keyType;
|
||||
private final TypeLiteral<V> valType;
|
||||
private String configKey;
|
||||
private boolean persist;
|
||||
private long maximumWeight;
|
||||
private long diskLimit;
|
||||
@@ -109,6 +110,13 @@ class CacheProvider<K, V> implements Provider<Cache<K, V>>, CacheBinding<K, V> {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CacheBinding<K, V> 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<K, V> implements Provider<Cache<K, V>>, CacheBinding<K, V> {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String configKey() {
|
||||
return configKey != null ? configKey : name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeLiteral<K> keyType() {
|
||||
return keyType;
|
||||
|
||||
@@ -60,6 +60,11 @@ class H2CacheBindingProxy<K, V> implements CacheBinding<K, V> {
|
||||
return source.name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String configKey() {
|
||||
return source.configKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeLiteral<K> keyType() {
|
||||
return source.keyType();
|
||||
@@ -85,6 +90,11 @@ class H2CacheBindingProxy<K, V> implements CacheBinding<K, V> {
|
||||
return source.loader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CacheBinding<K, V> configKey(String configKey) {
|
||||
throw new RuntimeException(MSG_NOT_SUPPORTED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CacheBinding<K, V> maximumWeight(long weight) {
|
||||
throw new RuntimeException(MSG_NOT_SUPPORTED);
|
||||
|
||||
@@ -155,7 +155,7 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
|
||||
@SuppressWarnings({"unchecked"})
|
||||
@Override
|
||||
public <K, V> Cache<K, V> build(CacheBinding<K, V> 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 <K, V> LoadingCache<K, V> build(CacheBinding<K, V> in, CacheLoader<K, V> 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);
|
||||
|
||||
@@ -55,7 +55,8 @@ class DefaultMemoryCacheFactory implements MemoryCacheFactory {
|
||||
private <K, V> CacheBuilder<K, V> create(CacheBinding<K, V> def) {
|
||||
CacheBuilder<K, V> 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);
|
||||
|
||||
Reference in New Issue
Block a user