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 distributed caching with 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 createDistributed(string $type = null): ICanCache
95 if ($type === Enum\Type::APCU) {
96 throw new InvalidCacheDriverException('apcu doesn\'t support distributed caching.');
99 return $this->create($type ?? $this->config->get('system', 'distributed_cache_driver', self::DEFAULT_TYPE));
103 * This method creates a CacheDriver for local caching with the given cache driver name
105 * @param string|null $type The cache type to create (default is per config)
107 * @return ICanCache The instance of the CacheDriver
109 * @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
110 * @throws CachePersistenceException In case the underlying cache has errors during persistence
112 public function createLocal(string $type = null): ICanCache
114 return $this->create($type ?? $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE));
118 * Creates a new Cache instance
120 * @param string $type The type of cache
124 * @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
125 * @throws CachePersistenceException In case the underlying cache has errors during persistence
127 protected function create(string $type): ICanCache
130 case Enum\Type::MEMCACHE:
131 $cache = new Type\MemcacheCache($this->hostname, $this->config);
133 case Enum\Type::MEMCACHED:
134 $cache = new Type\MemcachedCache($this->hostname, $this->config, $this->logger);
136 case Enum\Type::REDIS:
137 $cache = new Type\RedisCache($this->hostname, $this->config);
139 case Enum\Type::APCU:
140 $cache = new Type\APCuCache($this->hostname);
143 $cache = new Type\DatabaseCache($this->hostname, $this->dba);
146 $profiling = $this->config->get('system', 'profiling', false);
148 // In case profiling is enabled, wrap the ProfilerCache around the current cache
149 if (isset($profiling) && $profiling !== false) {
150 return new Type\ProfilerCacheDecorator($cache, $this->profiler);