3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Factory;
24 use Friendica\App\BaseURL;
25 use Friendica\Core\Cache;
26 use Friendica\Core\Cache\ICache;
27 use Friendica\Core\Config\IConfig;
28 use Friendica\Database\Database;
29 use Friendica\Util\Profiler;
30 use Psr\Log\LoggerInterface;
35 * @package Friendica\Core\Cache
37 * A basic class to generate a CacheDriver
42 * @var string The default cache if nothing set
44 const DEFAULT_TYPE = Cache\Type::DATABASE;
47 * @var IConfig The IConfiguration to read parameters out of the config
52 * @var Database The database connection in case that the cache is used the dba connection
57 * @var string The hostname, used as Prefix for Caching
62 * @var Profiler The optional profiler if the cached should be profiled
67 * @var LoggerInterface The Friendica Logger
71 public function __construct(BaseURL $baseURL, IConfig $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
73 $this->hostname = $baseURL->getHostname();
74 $this->config = $config;
76 $this->profiler = $profiler;
77 $this->logger = $logger;
81 * This method creates a CacheDriver for the given cache driver name
83 * @param string $type The cache type to create (default is per config)
85 * @return ICache The instance of the CacheDriver
86 * @throws \Exception The exception if something went wrong during the CacheDriver creation
88 public function create(string $type = null)
91 $type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
95 case Cache\Type::MEMCACHE:
96 $cache = new Cache\MemcacheCache($this->hostname, $this->config);
98 case Cache\Type::MEMCACHED:
99 $cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger);
101 case Cache\Type::REDIS:
102 $cache = new Cache\RedisCache($this->hostname, $this->config);
104 case Cache\Type::APCU:
105 $cache = new Cache\APCuCache($this->hostname);
108 $cache = new Cache\DatabaseCache($this->hostname, $this->dba);
111 $profiling = $this->config->get('system', 'profiling', false);
113 // In case profiling is enabled, wrap the ProfilerCache around the current cache
114 if (isset($profiling) && $profiling !== false) {
115 return new Cache\ProfilerCache($cache, $this->profiler);