Support for setting diskLimit from CacheModule bindings

For some persistent caches we may want a larger default diskLimit than
the overall default of 128MiB.

Change-Id: I9f8c85d7187d2edfe5ba48ea27c7b5f5e37e7abe
This commit is contained in:
Saša Živkov 2016-09-22 22:45:24 +02:00
parent 2c55373946
commit 9c87603a8b
3 changed files with 23 additions and 1 deletions

View File

@ -178,7 +178,7 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
public <K, V> LoadingCache<K, V> build(
CacheBinding<K, V> def,
CacheLoader<K, V> loader) {
long limit = config.getLong("cache", def.name(), "diskLimit", 128 << 20);
long limit = config.getLong("cache", def.name(), "diskLimit", def.diskLimit());
if (cacheDir == null || limit <= 0) {
return defaultFactory.build(def, loader);

View File

@ -26,6 +26,9 @@ public interface CacheBinding<K, V> {
/** Set the total size of the cache. */
CacheBinding<K, V> maximumWeight(long weight);
/** Set the total on-disk limit of the cache */
CacheBinding<K, V> diskLimit(long limit);
/** Set the time an element lives before being expired. */
CacheBinding<K, V> expireAfterWrite(long duration, TimeUnit durationUnits);
@ -39,6 +42,7 @@ public interface CacheBinding<K, V> {
TypeLiteral<K> keyType();
TypeLiteral<V> valueType();
long maximumWeight();
long diskLimit();
@Nullable Long expireAfterWrite(TimeUnit unit);
@Nullable Weigher<K, V> weigher();
@Nullable CacheLoader<K, V> loader();

View File

@ -38,6 +38,7 @@ class CacheProvider<K, V>
private final TypeLiteral<V> valType;
private boolean persist;
private long maximumWeight;
private long diskLimit;
private Long expireAfterWrite;
private Provider<CacheLoader<K, V>> loader;
private Provider<Weigher<K, V>> weigher;
@ -85,6 +86,15 @@ class CacheProvider<K, V>
return this;
}
@Override
public CacheBinding<K, V> diskLimit(long limit) {
Preconditions.checkState(!frozen, "binding frozen, cannot be modified");
Preconditions.checkState(persist,
"diskLimit supported for persistent caches only");
diskLimit = limit;
return this;
}
@Override
public CacheBinding<K, V> expireAfterWrite(long duration, TimeUnit unit) {
Preconditions.checkState(!frozen, "binding frozen, cannot be modified");
@ -129,6 +139,14 @@ class CacheProvider<K, V>
return maximumWeight;
}
@Override
public long diskLimit() {
if (diskLimit > 0) {
return diskLimit;
}
return 128 << 20;
}
@Override
@Nullable
public Long expireAfterWrite(TimeUnit unit) {