3 namespace Friendica\Factory;
5 use Friendica\App\BaseURL;
6 use Friendica\Core\Cache;
7 use Friendica\Core\Cache\ICache;
8 use Friendica\Core\Config\IConfig;
9 use Friendica\Database\Database;
10 use Friendica\Util\Profiler;
11 use Psr\Log\LoggerInterface;
16 * @package Friendica\Core\Cache
18 * A basic class to generate a CacheDriver
23 * @var string The default cache if nothing set
25 const DEFAULT_TYPE = Cache\Type::DATABASE;
28 * @var IConfig The IConfiguration to read parameters out of the config
33 * @var Database The database connection in case that the cache is used the dba connection
38 * @var string The hostname, used as Prefix for Caching
43 * @var Profiler The optional profiler if the cached should be profiled
48 * @var LoggerInterface The Friendica Logger
52 public function __construct(BaseURL $baseURL, IConfig $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
54 $this->hostname = $baseURL->getHostname();
55 $this->config = $config;
57 $this->profiler = $profiler;
58 $this->logger = $logger;
62 * This method creates a CacheDriver for the given cache driver name
64 * @param string $type The cache type to create (default is per config)
66 * @return ICache The instance of the CacheDriver
67 * @throws \Exception The exception if something went wrong during the CacheDriver creation
69 public function create(string $type = null)
72 $type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
76 case Cache\Type::MEMCACHE:
77 $cache = new Cache\MemcacheCache($this->hostname, $this->config);
79 case Cache\Type::MEMCACHED:
80 $cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger);
82 case Cache\Type::REDIS:
83 $cache = new Cache\RedisCache($this->hostname, $this->config);
85 case Cache\Type::APCU:
86 $cache = new Cache\APCuCache($this->hostname);
89 $cache = new Cache\DatabaseCache($this->hostname, $this->dba);
92 $profiling = $this->config->get('system', 'profiling', false);
94 // In case profiling is enabled, wrap the ProfilerCache around the current cache
95 if (isset($profiling) && $profiling !== false) {
96 return new Cache\ProfilerCache($cache, $this->profiler);