2018-06-28 16:57:17 -04:00
|
|
|
<?php
|
|
|
|
|
2019-03-03 09:05:35 -05:00
|
|
|
namespace Friendica\Factory;
|
2018-06-28 16:57:17 -04:00
|
|
|
|
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;
|
2019-08-03 14:48:56 -04: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 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
|
|
|
*/
|
2019-08-04 09:51:49 -04:00
|
|
|
const DEFAULT_TYPE = Cache\Cache::TYPE_DATABASE;
|
2019-08-03 14:48:56 -04: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 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) {
|
2019-08-04 09:51:49 -04:00
|
|
|
case Cache\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;
|
2019-08-04 09:51:49 -04:00
|
|
|
case Cache\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;
|
2019-08-04 09:51:49 -04:00
|
|
|
case Cache\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;
|
2019-08-04 09:51:49 -04:00
|
|
|
case Cache\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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|