3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\Core\Cache\Factory;
24 use Friendica\App\BaseURL;
25 use Friendica\Core\Cache\Enum;
26 use Friendica\Core\Cache\Capability\ICanCache;
27 use Friendica\Core\Cache\Exception\CachePersistenceException;
28 use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
29 use Friendica\Core\Cache\Type;
30 use Friendica\Core\Config\Capability\IManageConfigValues;
31 use Friendica\Database\Database;
32 use Friendica\Util\Profiler;
33 use Psr\Log\LoggerInterface;
38 * @package Friendica\Core\Cache
40 * A basic class to generate a CacheDriver
45 * @var string The default cache if nothing set
47 const DEFAULT_TYPE = Enum\Type::DATABASE;
50 * @var IManageConfigValues The IConfiguration to read parameters out of the config
55 * @var Database The database connection in case that the cache is used the dba connection
60 * @var string The hostname, used as Prefix for Caching
65 * @var Profiler The optional profiler if the cached should be profiled
70 * @var LoggerInterface The Friendica Logger
74 public function __construct(BaseURL $baseURL, IManageConfigValues $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
76 $this->hostname = $baseURL->getHostname();
77 $this->config = $config;
79 $this->profiler = $profiler;
80 $this->logger = $logger;
84 * This method creates a CacheDriver for the given cache driver name
86 * @param string|null $type The cache type to create (default is per config)
88 * @return ICanCache The instance of the CacheDriver
90 * @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
91 * @throws CachePersistenceException In case the underlying cache has errors during persistence
93 public function create(string $type = null): ICanCache
96 $type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
100 case Enum\Type::MEMCACHE:
101 $cache = new Type\MemcacheCache($this->hostname, $this->config);
103 case Enum\Type::MEMCACHED:
104 $cache = new Type\MemcachedCache($this->hostname, $this->config, $this->logger);
106 case Enum\Type::REDIS:
107 $cache = new Type\RedisCache($this->hostname, $this->config);
109 case Enum\Type::APCU:
110 $cache = new Type\APCuCache($this->hostname);
113 $cache = new Type\DatabaseCache($this->hostname, $this->dba);
116 $profiling = $this->config->get('system', 'profiling', false);
118 // In case profiling is enabled, wrap the ProfilerCache around the current cache
119 if (isset($profiling) && $profiling !== false) {
120 return new Type\ProfilerCacheDecorator($cache, $this->profiler);