]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Factory/Cache.php
Remove unused parameter
[friendica.git] / src / Core / Cache / Factory / Cache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Core\Cache\Factory;
23
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;
31
32 /**
33  * Class CacheFactory
34  *
35  * @package Friendica\Core\Cache
36  *
37  * A basic class to generate a CacheDriver
38  */
39 class Cache
40 {
41         /**
42          * @var string The default cache if nothing set
43          */
44         const DEFAULT_TYPE = Type\DatabaseCache::NAME;
45         /** @var ICanCreateInstances */
46         protected $instanceCreator;
47         /** @var IManageConfigValues */
48         protected $config;
49         /** @var Profiler */
50         protected $profiler;
51
52         public function __construct(ICanCreateInstances $instanceCreator, IManageConfigValues $config, Profiler $profiler)
53         {
54                 $this->config          = $config;
55                 $this->instanceCreator = $instanceCreator;
56                 $this->profiler        = $profiler;
57         }
58
59         /**
60          * This method creates a CacheDriver for distributed caching
61          *
62          * @return ICanCache  The instance of the CacheDriver
63          *
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
66          */
67         public function createDistributed(): ICanCache
68         {
69                 return $this->create($this->config->get('system', 'distributed_cache_driver', self::DEFAULT_TYPE));
70         }
71
72         /**
73          * This method creates a CacheDriver for local caching with the given cache driver name
74          *
75          * @param string|null $type The cache type to create (default is per config)
76          *
77          * @return ICanCache  The instance of the CacheDriver
78          *
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
81          */
82         public function createLocal(string $type = null): ICanCache
83         {
84                 return $this->create($type ?? $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE));
85         }
86
87         /**
88          * Creates a new Cache instance
89          *
90          * @param string $strategy The strategy, which cache instance should be used
91          *
92          * @return ICanCache
93          *
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
96          */
97         protected function create(string $strategy): ICanCache
98         {
99                 /** @var ICanCache $cache */
100                 $cache = $this->instanceCreator->create(ICanCache::class, $strategy);
101
102                 $profiling = $this->config->get('system', 'profiling', false);
103
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);
107                 } else {
108                         return $cache;
109                 }
110         }
111 }