2018-06-28 16:57:17 -04:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05: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 16:57:17 -04:00
|
|
|
|
2019-03-03 09:05:35 -05:00
|
|
|
namespace Friendica\Factory;
|
2018-06-28 16:57:17 -04:00
|
|
|
|
2019-08-15 11:36:07 -04:00
|
|
|
use Friendica\App\BaseURL;
|
2019-04-20 07:40:40 -04:00
|
|
|
use Friendica\Core\Cache;
|
2019-08-04 04:26:53 -04:00
|
|
|
use Friendica\Core\Cache\ICache;
|
2020-01-19 15:29:36 -05:00
|
|
|
use Friendica\Core\Config\IConfig;
|
2019-08-03 14:48:56 -04:00
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2018-06-28 16:57:17 -04:00
|
|
|
|
|
|
|
/**
|
2019-08-04 09:42:39 -04:00
|
|
|
* Class CacheFactory
|
2018-06-28 16:57:17 -04:00
|
|
|
*
|
|
|
|
* @package Friendica\Core\Cache
|
|
|
|
*
|
2018-07-05 01:59:56 -04:00
|
|
|
* A basic class to generate a CacheDriver
|
2018-06-28 16:57:17 -04:00
|
|
|
*/
|
2019-08-04 09:42:39 -04:00
|
|
|
class CacheFactory
|
2018-06-28 16:57:17 -04:00
|
|
|
{
|
2019-08-03 14:48:56 -04:00
|
|
|
/**
|
2019-08-04 09:42:39 -04:00
|
|
|
* @var string The default cache if nothing set
|
2019-08-03 14:48:56 -04:00
|
|
|
*/
|
2020-01-18 09:41:19 -05:00
|
|
|
const DEFAULT_TYPE = Cache\Type::DATABASE;
|
2019-08-03 14:48:56 -04:00
|
|
|
|
|
|
|
/**
|
2020-01-19 15:29:36 -05:00
|
|
|
* @var IConfig The IConfiguration to read parameters out of the config
|
2019-08-03 14:48:56 -04: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 15:29:36 -05:00
|
|
|
public function __construct(BaseURL $baseURL, IConfig $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
|
2019-08-03 14:48:56 -04:00
|
|
|
{
|
|
|
|
$this->hostname = $baseURL->getHostname();
|
2019-08-04 09:42:39 -04:00
|
|
|
$this->config = $config;
|
|
|
|
$this->dba = $dba;
|
2019-08-03 14:48:56 -04:00
|
|
|
$this->profiler = $profiler;
|
2019-08-04 09:42:39 -04:00
|
|
|
$this->logger = $logger;
|
2019-08-03 14:48:56 -04:00
|
|
|
}
|
|
|
|
|
2018-06-28 16:57:17 -04:00
|
|
|
/**
|
2018-07-05 01:59:56 -04:00
|
|
|
* This method creates a CacheDriver for the given cache driver name
|
2018-06-28 16:57:17 -04:00
|
|
|
*
|
2019-08-04 09:42:39 -04:00
|
|
|
* @param string $type The cache type to create (default is per config)
|
|
|
|
*
|
2019-08-04 04:26:53 -04:00
|
|
|
* @return ICache The instance of the CacheDriver
|
2018-06-28 16:57:17 -04:00
|
|
|
* @throws \Exception The exception if something went wrong during the CacheDriver creation
|
|
|
|
*/
|
2019-08-04 09:42:39 -04:00
|
|
|
public function create(string $type = null)
|
2019-08-03 14:48:56 -04:00
|
|
|
{
|
2019-08-04 09:42:39 -04:00
|
|
|
if (empty($type)) {
|
|
|
|
$type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
|
|
|
|
}
|
2018-06-28 16:57:17 -04:00
|
|
|
|
2019-08-04 09:42:39 -04:00
|
|
|
switch ($type) {
|
2020-01-18 09:41:19 -05:00
|
|
|
case Cache\Type::MEMCACHE:
|
2019-08-04 04:26:53 -04:00
|
|
|
$cache = new Cache\MemcacheCache($this->hostname, $this->config);
|
2018-06-28 16:57:17 -04:00
|
|
|
break;
|
2020-01-18 09:41:19 -05:00
|
|
|
case Cache\Type::MEMCACHED:
|
2019-08-04 04:26:53 -04:00
|
|
|
$cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger);
|
2018-06-28 16:57:17 -04:00
|
|
|
break;
|
2020-01-18 09:41:19 -05:00
|
|
|
case Cache\Type::REDIS:
|
2019-08-04 04:26:53 -04:00
|
|
|
$cache = new Cache\RedisCache($this->hostname, $this->config);
|
2018-06-28 16:57:17 -04:00
|
|
|
break;
|
2020-01-18 09:41:19 -05:00
|
|
|
case Cache\Type::APCU:
|
2019-08-03 14:48:56 -04:00
|
|
|
$cache = new Cache\APCuCache($this->hostname);
|
2019-04-20 11:37:57 -04:00
|
|
|
break;
|
2018-06-28 16:57:17 -04:00
|
|
|
default:
|
2019-08-04 04:26:53 -04:00
|
|
|
$cache = new Cache\DatabaseCache($this->hostname, $this->dba);
|
2019-08-03 14:48:56 -04: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 09:42:39 -04:00
|
|
|
return new Cache\ProfilerCache($cache, $this->profiler);
|
2019-08-03 14:48:56 -04:00
|
|
|
} else {
|
|
|
|
return $cache;
|
2018-06-28 16:57:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|