Skip to content

Commit

Permalink
API Opt-out of in-memory caching factory
Browse files Browse the repository at this point in the history
In-memory caches are typically more resource constrained (number of items and storage space).
Give cache consumers an opt-out if they are expecting to create large caches with long lifetimes.
Use case: silverstripe/silverstripe-assets#282
chillu authored and Maxime Rainville committed Jun 7, 2019
1 parent e8fa3d8 commit 8324235
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Core/Cache/DefaultCacheFactory.php
Original file line number Diff line number Diff line change
@@ -56,8 +56,12 @@ public function create($service, array $args = array())
$directory = isset($args['directory']) ? $args['directory'] : null;
$version = isset($args['version']) ? $args['version'] : null;

// In-memory caches are typically more resource constrained (number of items and storage space).
// Give cache consumers an opt-out if they are expecting to create large caches with long lifetimes.
$useInMemoryCache = isset($args['useInMemoryCache']) ? $args['useInMemoryCache'] : true;

// Check support
$apcuSupported = $this->isAPCUSupported();
$apcuSupported = ($this->isAPCUSupported() && $useInMemoryCache);
$phpFilesSupported = $this->isPHPFilesSupported();

// If apcu isn't supported, phpfiles is the next best preference
@@ -72,8 +76,11 @@ public function create($service, array $args = array())
}

// Chain this cache with ApcuCache
// Note that the cache lifetime will be shorter there by default, to ensure there's enough
// resources for "hot cache" items in APCu as a resource constrained in memory cache.
$apcuNamespace = $namespace . ($namespace ? '_' : '') . md5(BASE_PATH);
$apcu = $this->createCache(ApcuCache::class, [$apcuNamespace, (int) $defaultLifetime / 5, $version]);

return $this->createCache(ChainCache::class, [[$apcu, $fs]]);
}

0 comments on commit 8324235

Please sign in to comment.