2018-06-28 22:57:17 +02:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-06-28 22:57:17 +02:00
|
|
|
|
2019-03-03 15:05:35 +01:00
|
|
|
namespace Friendica\Factory;
|
2018-06-28 22:57:17 +02:00
|
|
|
|
2019-08-15 17:36:07 +02:00
|
|
|
use Friendica\App\BaseURL;
|
2019-04-20 13:40:40 +02:00
|
|
|
use Friendica\Core\Cache;
|
2019-08-04 10:26:53 +02:00
|
|
|
use Friendica\Core\Cache\ICache;
|
2020-01-19 21:29:36 +01:00
|
|
|
use Friendica\Core\Config\IConfig;
|
2019-08-03 20:48:56 +02:00
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2018-06-28 22:57:17 +02:00
|
|
|
|
|
|
|
/**
|
2019-08-04 15:42:39 +02:00
|
|
|
* Class CacheFactory
|
2018-06-28 22:57:17 +02:00
|
|
|
*
|
|
|
|
* @package Friendica\Core\Cache
|
|
|
|
*
|
2018-07-05 07:59:56 +02:00
|
|
|
* A basic class to generate a CacheDriver
|
2018-06-28 22:57:17 +02:00
|
|
|
*/
|
2019-08-04 15:42:39 +02:00
|
|
|
class CacheFactory
|
2018-06-28 22:57:17 +02:00
|
|
|
{
|
2019-08-03 20:48:56 +02:00
|
|
|
/**
|
2019-08-04 15:42:39 +02:00
|
|
|
* @var string The default cache if nothing set
|
2019-08-03 20:48:56 +02:00
|
|
|
*/
|
2020-01-18 15:41:19 +01:00
|
|
|
const DEFAULT_TYPE = Cache\Type::DATABASE;
|
2019-08-03 20:48:56 +02:00
|
|
|
|
|
|
|
/**
|
2020-01-19 21:29:36 +01:00
|
|
|
* @var IConfig The IConfiguration to read parameters out of the config
|
2019-08-03 20:48:56 +02:00
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Database The database connection in case that the cache is used the dba connection
|
|
|
|
*/
|
|
|
|
private $dba;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string The hostname, used as Prefix for Caching
|
|
|
|
*/
|
|
|
|
private $hostname;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Profiler The optional profiler if the cached should be profiled
|
|
|
|
*/
|
|
|
|
private $profiler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var LoggerInterface The Friendica Logger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
2020-01-19 21:29:36 +01:00
|
|
|
public function __construct(BaseURL $baseURL, IConfig $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
|
2019-08-03 20:48:56 +02:00
|
|
|
{
|
|
|
|
$this->hostname = $baseURL->getHostname();
|
2019-08-04 15:42:39 +02:00
|
|
|
$this->config = $config;
|
|
|
|
$this->dba = $dba;
|
2019-08-03 20:48:56 +02:00
|
|
|
$this->profiler = $profiler;
|
2019-08-04 15:42:39 +02:00
|
|
|
$this->logger = $logger;
|
2019-08-03 20:48:56 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 22:57:17 +02:00
|
|
|
/**
|
2018-07-05 07:59:56 +02:00
|
|
|
* This method creates a CacheDriver for the given cache driver name
|
2018-06-28 22:57:17 +02:00
|
|
|
*
|
2019-08-04 15:42:39 +02:00
|
|
|
* @param string $type The cache type to create (default is per config)
|
|
|
|
*
|
2019-08-04 10:26:53 +02:00
|
|
|
* @return ICache The instance of the CacheDriver
|
2018-06-28 22:57:17 +02:00
|
|
|
* @throws \Exception The exception if something went wrong during the CacheDriver creation
|
|
|
|
*/
|
2019-08-04 15:42:39 +02:00
|
|
|
public function create(string $type = null)
|
2019-08-03 20:48:56 +02:00
|
|
|
{
|
2019-08-04 15:42:39 +02:00
|
|
|
if (empty($type)) {
|
|
|
|
$type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
|
|
|
|
}
|
2018-06-28 22:57:17 +02:00
|
|
|
|
2019-08-04 15:42:39 +02:00
|
|
|
switch ($type) {
|
2020-01-18 15:41:19 +01:00
|
|
|
case Cache\Type::MEMCACHE:
|
2019-08-04 10:26:53 +02:00
|
|
|
$cache = new Cache\MemcacheCache($this->hostname, $this->config);
|
2018-06-28 22:57:17 +02:00
|
|
|
break;
|
2020-01-18 15:41:19 +01:00
|
|
|
case Cache\Type::MEMCACHED:
|
2019-08-04 10:26:53 +02:00
|
|
|
$cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger);
|
2018-06-28 22:57:17 +02:00
|
|
|
break;
|
2020-01-18 15:41:19 +01:00
|
|
|
case Cache\Type::REDIS:
|
2019-08-04 10:26:53 +02:00
|
|
|
$cache = new Cache\RedisCache($this->hostname, $this->config);
|
2018-06-28 22:57:17 +02:00
|
|
|
break;
|
2020-01-18 15:41:19 +01:00
|
|
|
case Cache\Type::APCU:
|
2019-08-03 20:48:56 +02:00
|
|
|
$cache = new Cache\APCuCache($this->hostname);
|
2019-04-20 17:37:57 +02:00
|
|
|
break;
|
2018-06-28 22:57:17 +02:00
|
|
|
default:
|
2019-08-04 10:26:53 +02:00
|
|
|
$cache = new Cache\DatabaseCache($this->hostname, $this->dba);
|
2019-08-03 20:48:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$profiling = $this->config->get('system', 'profiling', false);
|
|
|
|
|
|
|
|
// In case profiling is enabled, wrap the ProfilerCache around the current cache
|
|
|
|
if (isset($profiling) && $profiling !== false) {
|
2019-08-04 15:42:39 +02:00
|
|
|
return new Cache\ProfilerCache($cache, $this->profiler);
|
2019-08-03 20:48:56 +02:00
|
|
|
} else {
|
|
|
|
return $cache;
|
2018-06-28 22:57:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|