2018-06-28 22:57:17 +02:00
|
|
|
<?php
|
|
|
|
|
2019-03-03 15:05:35 +01:00
|
|
|
namespace Friendica\Factory;
|
2018-06-28 22:57:17 +02:00
|
|
|
|
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;
|
2019-08-03 20:48:56 +02:00
|
|
|
use Friendica\Core\Config\Configuration;
|
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Friendica\Util\BaseURL;
|
|
|
|
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
|
|
|
*/
|
2019-08-04 15:51:49 +02:00
|
|
|
const DEFAULT_TYPE = Cache\Cache::TYPE_DATABASE;
|
2019-08-03 20:48:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Configuration The configuration to read parameters out of the config
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
public function __construct(BaseURL $baseURL, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
|
|
|
|
{
|
|
|
|
$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) {
|
2019-08-04 15:51:49 +02:00
|
|
|
case Cache\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;
|
2019-08-04 15:51:49 +02:00
|
|
|
case Cache\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;
|
2019-08-04 15:51:49 +02:00
|
|
|
case Cache\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;
|
2019-08-04 15:51:49 +02:00
|
|
|
case Cache\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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|