2013-07-16 09:34:22 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2016-07-21 11:07:57 -04:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
|
*
|
2020-04-29 05:57:22 -04:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 09:56:42 -05:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 13:56:05 -04:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 06:44:34 -04:00
|
|
|
* @author Markus Goetz <markus@woboq.com>
|
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2021-06-04 15:52:51 -04:00
|
|
|
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
2016-07-21 12:13:36 -04:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 09:02:16 -05:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2019-12-03 13:57:53 -05:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-05-26 13:56:05 -04:00
|
|
|
* @author Stefan Weil <sw@weilnetz.de>
|
2020-12-16 08:54:15 -05:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2015-03-26 06:44:34 -04:00
|
|
|
*
|
|
|
|
|
* @license AGPL-3.0
|
|
|
|
|
*
|
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 13:57:53 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 06:44:34 -04:00
|
|
|
*
|
2013-07-16 09:34:22 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Memcache;
|
|
|
|
|
|
2022-01-19 17:30:34 -05:00
|
|
|
use OCP\Profiler\IProfiler;
|
2018-01-16 14:44:51 -05:00
|
|
|
use OCP\ICache;
|
|
|
|
|
use OCP\ICacheFactory;
|
|
|
|
|
use OCP\IMemcache;
|
2022-03-17 11:42:53 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2014-01-24 10:01:19 -05:00
|
|
|
|
|
|
|
|
class Factory implements ICacheFactory {
|
2020-04-10 10:54:27 -04:00
|
|
|
public const NULL_CACHE = NullCache::class;
|
2015-01-14 13:25:00 -05:00
|
|
|
|
2022-01-19 17:30:34 -05:00
|
|
|
private string $globalPrefix;
|
2014-01-06 07:11:38 -05:00
|
|
|
|
2022-03-17 11:42:53 -04:00
|
|
|
private LoggerInterface $logger;
|
2015-07-15 10:21:07 -04:00
|
|
|
|
2015-01-14 13:25:00 -05:00
|
|
|
/**
|
2022-01-19 17:30:34 -05:00
|
|
|
* @var ?class-string<ICache> $localCacheClass
|
|
|
|
|
*/
|
|
|
|
|
private ?string $localCacheClass;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var ?class-string<ICache> $distributedCacheClass
|
2015-01-14 13:25:00 -05:00
|
|
|
*/
|
2022-01-19 17:30:34 -05:00
|
|
|
private ?string $distributedCacheClass;
|
2015-01-14 13:25:00 -05:00
|
|
|
|
|
|
|
|
/**
|
2022-01-19 17:30:34 -05:00
|
|
|
* @var ?class-string<IMemcache> $lockingCacheClass
|
2015-01-14 13:25:00 -05:00
|
|
|
*/
|
2022-01-19 17:30:34 -05:00
|
|
|
private ?string $lockingCacheClass;
|
|
|
|
|
|
|
|
|
|
private string $logFile;
|
|
|
|
|
|
|
|
|
|
private IProfiler $profiler;
|
2015-01-14 13:25:00 -05:00
|
|
|
|
2015-05-26 08:41:37 -04:00
|
|
|
/**
|
2022-01-19 17:30:34 -05:00
|
|
|
* @param string $globalPrefix
|
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
* @param ?class-string<ICache> $localCacheClass
|
|
|
|
|
* @param ?class-string<ICache> $distributedCacheClass
|
|
|
|
|
* @param ?class-string<IMemcache> $lockingCacheClass
|
|
|
|
|
* @param string $logFile
|
2015-05-26 08:41:37 -04:00
|
|
|
*/
|
2022-01-19 17:30:34 -05:00
|
|
|
public function __construct(string $globalPrefix, LoggerInterface $logger, IProfiler $profiler,
|
|
|
|
|
?string $localCacheClass = null, ?string $distributedCacheClass = null, ?string $lockingCacheClass = null, string $logFile = '') {
|
2015-07-15 10:21:07 -04:00
|
|
|
$this->logger = $logger;
|
2021-12-01 10:30:45 -05:00
|
|
|
$this->logFile = $logFile;
|
2014-01-06 07:11:38 -05:00
|
|
|
$this->globalPrefix = $globalPrefix;
|
2015-01-14 13:25:00 -05:00
|
|
|
|
2015-06-09 09:22:33 -04:00
|
|
|
if (!$localCacheClass) {
|
2015-01-14 13:25:00 -05:00
|
|
|
$localCacheClass = self::NULL_CACHE;
|
|
|
|
|
}
|
2015-06-09 09:22:33 -04:00
|
|
|
if (!$distributedCacheClass) {
|
2015-01-14 13:25:00 -05:00
|
|
|
$distributedCacheClass = $localCacheClass;
|
|
|
|
|
}
|
2015-06-09 09:22:33 -04:00
|
|
|
|
2015-07-15 10:21:07 -04:00
|
|
|
$missingCacheMessage = 'Memcache {class} not available for {use} cache';
|
|
|
|
|
$missingCacheHint = 'Is the matching PHP module installed and enabled?';
|
2017-01-05 05:57:18 -05:00
|
|
|
if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) {
|
2021-06-29 19:20:33 -04:00
|
|
|
throw new \OCP\HintException(strtr($missingCacheMessage, [
|
2021-02-23 12:50:36 -05:00
|
|
|
'{class}' => $localCacheClass, '{use}' => 'local'
|
|
|
|
|
]), $missingCacheHint);
|
2015-06-09 09:22:33 -04:00
|
|
|
}
|
2017-01-05 05:57:18 -05:00
|
|
|
if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) {
|
2021-06-29 19:20:33 -04:00
|
|
|
throw new \OCP\HintException(strtr($missingCacheMessage, [
|
2021-02-23 12:50:36 -05:00
|
|
|
'{class}' => $distributedCacheClass, '{use}' => 'distributed'
|
|
|
|
|
]), $missingCacheHint);
|
2015-06-09 09:22:33 -04:00
|
|
|
}
|
2021-07-05 17:11:13 -04:00
|
|
|
if (!($lockingCacheClass && class_exists($lockingCacheClass) && $lockingCacheClass::isAvailable())) {
|
2016-04-07 13:51:27 -04:00
|
|
|
// don't fallback since the fallback might not be suitable for storing lock
|
2015-07-15 10:21:07 -04:00
|
|
|
$lockingCacheClass = self::NULL_CACHE;
|
2015-05-26 08:41:37 -04:00
|
|
|
}
|
2015-07-15 10:21:07 -04:00
|
|
|
|
2015-01-14 13:25:00 -05:00
|
|
|
$this->localCacheClass = $localCacheClass;
|
|
|
|
|
$this->distributedCacheClass = $distributedCacheClass;
|
2015-05-26 08:41:37 -04:00
|
|
|
$this->lockingCacheClass = $lockingCacheClass;
|
2022-01-19 17:30:34 -05:00
|
|
|
$this->profiler = $profiler;
|
2015-05-26 08:41:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* create a cache instance for storing locks
|
|
|
|
|
*
|
|
|
|
|
* @param string $prefix
|
2018-01-16 14:44:51 -05:00
|
|
|
* @return IMemcache
|
2015-05-26 08:41:37 -04:00
|
|
|
*/
|
2018-01-16 14:44:51 -05:00
|
|
|
public function createLocking(string $prefix = ''): IMemcache {
|
2022-01-19 17:30:34 -05:00
|
|
|
assert($this->lockingCacheClass !== null);
|
|
|
|
|
$cache = new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix);
|
|
|
|
|
if ($this->profiler->isEnabled() && $this->lockingCacheClass === '\OC\Memcache\Redis') {
|
|
|
|
|
// We only support the profiler with Redis
|
|
|
|
|
$cache = new ProfilerWrapperCache($cache, 'Locking');
|
|
|
|
|
$this->profiler->add($cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->lockingCacheClass === Redis::class &&
|
|
|
|
|
$this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
|
|
|
|
|
$cache = new LoggerWrapperCache($cache, $this->logFile);
|
|
|
|
|
}
|
|
|
|
|
return $cache;
|
2014-01-06 07:11:38 -05:00
|
|
|
}
|
|
|
|
|
|
2013-07-16 09:34:22 -04:00
|
|
|
/**
|
2015-01-14 13:25:00 -05:00
|
|
|
* create a distributed cache instance
|
2013-07-16 09:34:22 -04:00
|
|
|
*
|
2013-07-16 10:08:37 -04:00
|
|
|
* @param string $prefix
|
2018-01-16 14:44:51 -05:00
|
|
|
* @return ICache
|
2013-07-16 09:34:22 -04:00
|
|
|
*/
|
2018-01-16 14:44:51 -05:00
|
|
|
public function createDistributed(string $prefix = ''): ICache {
|
2022-01-19 17:30:34 -05:00
|
|
|
assert($this->distributedCacheClass !== null);
|
|
|
|
|
$cache = new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix);
|
|
|
|
|
if ($this->profiler->isEnabled() && $this->distributedCacheClass === '\OC\Memcache\Redis') {
|
|
|
|
|
// We only support the profiler with Redis
|
|
|
|
|
$cache = new ProfilerWrapperCache($cache, 'Distributed');
|
|
|
|
|
$this->profiler->add($cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->distributedCacheClass === Redis::class && $this->logFile !== ''
|
|
|
|
|
&& is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
|
|
|
|
|
$cache = new LoggerWrapperCache($cache, $this->logFile);
|
|
|
|
|
}
|
|
|
|
|
return $cache;
|
2015-01-14 13:25:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* create a local cache instance
|
|
|
|
|
*
|
|
|
|
|
* @param string $prefix
|
2018-01-16 14:44:51 -05:00
|
|
|
* @return ICache
|
2015-01-14 13:25:00 -05:00
|
|
|
*/
|
2018-01-16 14:44:51 -05:00
|
|
|
public function createLocal(string $prefix = ''): ICache {
|
2022-01-19 17:30:34 -05:00
|
|
|
assert($this->localCacheClass !== null);
|
|
|
|
|
$cache = new $this->localCacheClass($this->globalPrefix . '/' . $prefix);
|
|
|
|
|
if ($this->profiler->isEnabled() && $this->localCacheClass === '\OC\Memcache\Redis') {
|
|
|
|
|
// We only support the profiler with Redis
|
|
|
|
|
$cache = new ProfilerWrapperCache($cache, 'Local');
|
|
|
|
|
$this->profiler->add($cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->localCacheClass === Redis::class && $this->logFile !== ''
|
|
|
|
|
&& is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
|
|
|
|
|
$cache = new LoggerWrapperCache($cache, $this->logFile);
|
|
|
|
|
}
|
|
|
|
|
return $cache;
|
2015-01-14 13:25:00 -05:00
|
|
|
}
|
|
|
|
|
|
2013-07-16 09:34:22 -04:00
|
|
|
/**
|
2015-01-14 13:25:00 -05:00
|
|
|
* check memcache availability
|
2013-07-16 09:34:22 -04:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2018-01-16 14:44:51 -05:00
|
|
|
public function isAvailable(): bool {
|
2015-01-14 13:25:00 -05:00
|
|
|
return ($this->distributedCacheClass !== self::NULL_CACHE);
|
2013-07-16 09:34:22 -04:00
|
|
|
}
|
2013-08-17 10:01:37 -04:00
|
|
|
|
|
|
|
|
/**
|
2015-01-14 13:25:00 -05:00
|
|
|
* @see \OC\Memcache\Factory::createLocal()
|
2013-08-17 10:01:37 -04:00
|
|
|
* @param string $prefix
|
2018-01-16 14:44:51 -05:00
|
|
|
* @return ICache
|
2013-08-17 10:01:37 -04:00
|
|
|
*/
|
2018-01-16 14:44:51 -05:00
|
|
|
public function createLowLatency(string $prefix = ''): ICache {
|
2015-01-14 13:25:00 -05:00
|
|
|
return $this->createLocal($prefix);
|
2013-08-17 10:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-13 13:18:04 -04:00
|
|
|
* Check if a local memory cache backend is available
|
2013-08-17 10:01:37 -04:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2018-03-13 13:18:04 -04:00
|
|
|
public function isLocalCacheAvailable(): bool {
|
2015-01-14 13:25:00 -05:00
|
|
|
return ($this->localCacheClass !== self::NULL_CACHE);
|
2013-08-17 10:01:37 -04:00
|
|
|
}
|
2013-07-16 09:34:22 -04:00
|
|
|
}
|