|
|
|
|
@ -16,7 +16,7 @@ use Psr\Log\LoggerInterface;
|
|
|
|
|
class Factory implements ICacheFactory {
|
|
|
|
|
public const NULL_CACHE = NullCache::class;
|
|
|
|
|
|
|
|
|
|
private string $globalPrefix;
|
|
|
|
|
private ?string $globalPrefix = null;
|
|
|
|
|
|
|
|
|
|
private LoggerInterface $logger;
|
|
|
|
|
|
|
|
|
|
@ -40,17 +40,23 @@ class Factory implements ICacheFactory {
|
|
|
|
|
private IProfiler $profiler;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $globalPrefix
|
|
|
|
|
* @param callable $globalPrefixClosure
|
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
* @param ?class-string<ICache> $localCacheClass
|
|
|
|
|
* @param ?class-string<ICache> $distributedCacheClass
|
|
|
|
|
* @param ?class-string<IMemcache> $lockingCacheClass
|
|
|
|
|
* @param string $logFile
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(string $globalPrefix, LoggerInterface $logger, IProfiler $profiler,
|
|
|
|
|
?string $localCacheClass = null, ?string $distributedCacheClass = null, ?string $lockingCacheClass = null, string $logFile = '') {
|
|
|
|
|
public function __construct(
|
|
|
|
|
private $globalPrefixClosure,
|
|
|
|
|
LoggerInterface $logger,
|
|
|
|
|
IProfiler $profiler,
|
|
|
|
|
?string $localCacheClass = null,
|
|
|
|
|
?string $distributedCacheClass = null,
|
|
|
|
|
?string $lockingCacheClass = null,
|
|
|
|
|
string $logFile = ''
|
|
|
|
|
) {
|
|
|
|
|
$this->logFile = $logFile;
|
|
|
|
|
$this->globalPrefix = $globalPrefix;
|
|
|
|
|
|
|
|
|
|
if (!$localCacheClass) {
|
|
|
|
|
$localCacheClass = self::NULL_CACHE;
|
|
|
|
|
@ -59,6 +65,7 @@ class Factory implements ICacheFactory {
|
|
|
|
|
if (!$distributedCacheClass) {
|
|
|
|
|
$distributedCacheClass = $localCacheClass;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$distributedCacheClass = ltrim($distributedCacheClass, '\\');
|
|
|
|
|
|
|
|
|
|
$missingCacheMessage = 'Memcache {class} not available for {use} cache';
|
|
|
|
|
@ -85,6 +92,13 @@ class Factory implements ICacheFactory {
|
|
|
|
|
$this->profiler = $profiler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getGlobalPrefix(): ?string {
|
|
|
|
|
if (is_null($this->globalPrefix)) {
|
|
|
|
|
$this->globalPrefix = ($this->globalPrefixClosure)();
|
|
|
|
|
}
|
|
|
|
|
return $this->globalPrefix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* create a cache instance for storing locks
|
|
|
|
|
*
|
|
|
|
|
@ -92,8 +106,13 @@ class Factory implements ICacheFactory {
|
|
|
|
|
* @return IMemcache
|
|
|
|
|
*/
|
|
|
|
|
public function createLocking(string $prefix = ''): IMemcache {
|
|
|
|
|
$globalPrefix = $this->getGlobalPrefix();
|
|
|
|
|
if (is_null($globalPrefix)) {
|
|
|
|
|
return new ArrayCache($prefix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert($this->lockingCacheClass !== null);
|
|
|
|
|
$cache = new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix);
|
|
|
|
|
$cache = new $this->lockingCacheClass($globalPrefix . '/' . $prefix);
|
|
|
|
|
if ($this->lockingCacheClass === Redis::class && $this->profiler->isEnabled()) {
|
|
|
|
|
// We only support the profiler with Redis
|
|
|
|
|
$cache = new ProfilerWrapperCache($cache, 'Locking');
|
|
|
|
|
@ -114,8 +133,13 @@ class Factory implements ICacheFactory {
|
|
|
|
|
* @return ICache
|
|
|
|
|
*/
|
|
|
|
|
public function createDistributed(string $prefix = ''): ICache {
|
|
|
|
|
$globalPrefix = $this->getGlobalPrefix();
|
|
|
|
|
if (is_null($globalPrefix)) {
|
|
|
|
|
return new ArrayCache($prefix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert($this->distributedCacheClass !== null);
|
|
|
|
|
$cache = new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix);
|
|
|
|
|
$cache = new $this->distributedCacheClass($globalPrefix . '/' . $prefix);
|
|
|
|
|
if ($this->distributedCacheClass === Redis::class && $this->profiler->isEnabled()) {
|
|
|
|
|
// We only support the profiler with Redis
|
|
|
|
|
$cache = new ProfilerWrapperCache($cache, 'Distributed');
|
|
|
|
|
@ -136,8 +160,13 @@ class Factory implements ICacheFactory {
|
|
|
|
|
* @return ICache
|
|
|
|
|
*/
|
|
|
|
|
public function createLocal(string $prefix = ''): ICache {
|
|
|
|
|
$globalPrefix = $this->getGlobalPrefix();
|
|
|
|
|
if (is_null($globalPrefix)) {
|
|
|
|
|
return new ArrayCache($prefix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert($this->localCacheClass !== null);
|
|
|
|
|
$cache = new $this->localCacheClass($this->globalPrefix . '/' . $prefix);
|
|
|
|
|
$cache = new $this->localCacheClass($globalPrefix . '/' . $prefix);
|
|
|
|
|
if ($this->localCacheClass === Redis::class && $this->profiler->isEnabled()) {
|
|
|
|
|
// We only support the profiler with Redis
|
|
|
|
|
$cache = new ProfilerWrapperCache($cache, 'Local');
|
|
|
|
|
|