Merge "Make H2 database cache size configurable"
This commit is contained in:
commit
e7ae7f4d2f
@ -528,6 +528,20 @@ If not absolute, the path is resolved relative to `$site_path`.
|
||||
+
|
||||
Default is unset, no disk cache.
|
||||
|
||||
[[cache.h2CacheSize]]cache.h2CacheSize::
|
||||
+
|
||||
The size of the H2 database cache, in bytes, used for each persistent cache.
|
||||
This can be used to limit the H2 cache size and thus prevent out-of-memory
|
||||
caused by the H2 database using too much memory.
|
||||
+
|
||||
Technically the H2 cache size is configured using the CACHE_SIZE parameter in
|
||||
the H2 JDBC connection URL, as described
|
||||
link:http://www.h2database.com/html/features.html#cache_settings[here]
|
||||
+
|
||||
Default is unset, no cache size limit.
|
||||
+
|
||||
Common unit suffixes of 'k', 'm', or 'g' are supported.
|
||||
|
||||
[[cache.name.maxAge]]cache.<name>.maxAge::
|
||||
+
|
||||
Maximum age to keep an entry in the cache. Entries are removed from
|
||||
|
@ -58,6 +58,7 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
|
||||
private final DynamicMap<Cache<?, ?>> cacheMap;
|
||||
private final ExecutorService executor;
|
||||
private final ScheduledExecutorService cleanup;
|
||||
private final long h2CacheSize;
|
||||
|
||||
@Inject
|
||||
H2CacheFactory(
|
||||
@ -68,6 +69,7 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
|
||||
defaultFactory = defaultCacheFactory;
|
||||
config = cfg;
|
||||
cacheDir = getCacheDir(site, cfg.getString("cache", null, "directory"));
|
||||
h2CacheSize = cfg.getLong("cache", null, "h2CacheSize", -1);
|
||||
caches = Lists.newLinkedList();
|
||||
this.cacheMap = cacheMap;
|
||||
|
||||
@ -220,8 +222,14 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
|
||||
TypeLiteral<K> keyType,
|
||||
long maxSize,
|
||||
Long expireAfterWrite) {
|
||||
String url = "jdbc:h2:" + cacheDir.resolve(name).toUri();
|
||||
return new SqlStore<>(url, keyType, maxSize,
|
||||
StringBuilder url = new StringBuilder();
|
||||
url.append("jdbc:h2:").append(cacheDir.resolve(name).toUri());
|
||||
if (h2CacheSize >= 0) {
|
||||
url.append(";CACHE_SIZE=");
|
||||
// H2 CACHE_SIZE is always given in KB
|
||||
url.append(h2CacheSize / 1024);
|
||||
}
|
||||
return new SqlStore<>(url.toString(), keyType, maxSize,
|
||||
expireAfterWrite == null ? 0 : expireAfterWrite.longValue());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user