3 * @copyright Copyright (C) 2010-2023, 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\Core\Cache\Capability\ICanCache;
25 use Friendica\Core\Cache\Exception\CachePersistenceException;
26 use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
27 use Friendica\Core\Cache\Type;
28 use Friendica\Core\Config\Capability\IManageConfigValues;
29 use Friendica\Core\Hooks\Capability\ICanCreateInstances;
30 use Friendica\Util\Profiler;
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 = Type\DatabaseCache::NAME;
45 /** @var ICanCreateInstances */
46 protected $instanceCreator;
47 /** @var IManageConfigValues */
52 public function __construct(ICanCreateInstances $instanceCreator, IManageConfigValues $config, Profiler $profiler)
54 $this->config = $config;
55 $this->instanceCreator = $instanceCreator;
56 $this->profiler = $profiler;
60 * This method creates a CacheDriver for distributed caching
62 * @return ICanCache The instance of the CacheDriver
64 * @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
65 * @throws CachePersistenceException In case the underlying cache has errors during persistence
67 public function createDistributed(): ICanCache
69 return $this->create($this->config->get('system', 'distributed_cache_driver', self::DEFAULT_TYPE));
73 * This method creates a CacheDriver for local caching with the given cache driver name
75 * @param string|null $type The cache type to create (default is per config)
77 * @return ICanCache The instance of the CacheDriver
79 * @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
80 * @throws CachePersistenceException In case the underlying cache has errors during persistence
82 public function createLocal(string $type = null): ICanCache
84 return $this->create($type ?? $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE));
88 * Creates a new Cache instance
90 * @param string $strategy The strategy, which cache instance should be used
94 * @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
95 * @throws CachePersistenceException In case the underlying cache has errors during persistence
97 protected function create(string $strategy): ICanCache
99 /** @var ICanCache $cache */
100 $cache = $this->instanceCreator->create(ICanCache::class, $strategy);
102 $profiling = $this->config->get('system', 'profiling', false);
104 // In case profiling is enabled, wrap the ProfilerCache around the current cache
105 if (isset($profiling) && $profiling !== false) {
106 return new Type\ProfilerCacheDecorator($cache, $this->profiler);