]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Factory/Cache.php
Merge pull request #11684 from MrPetovan/bug/11651-ap-fetch-queue
[friendica.git] / src / Core / Cache / Factory / Cache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\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;
34
35 /**
36  * Class CacheFactory
37  *
38  * @package Friendica\Core\Cache
39  *
40  * A basic class to generate a CacheDriver
41  */
42 class Cache
43 {
44         /**
45          * @var string The default cache if nothing set
46          */
47         const DEFAULT_TYPE = Enum\Type::DATABASE;
48
49         /**
50          * @var IManageConfigValues The IConfiguration to read parameters out of the config
51          */
52         private $config;
53
54         /**
55          * @var Database The database connection in case that the cache is used the dba connection
56          */
57         private $dba;
58
59         /**
60          * @var string The hostname, used as Prefix for Caching
61          */
62         private $hostname;
63
64         /**
65          * @var Profiler The optional profiler if the cached should be profiled
66          */
67         private $profiler;
68
69         /**
70          * @var LoggerInterface The Friendica Logger
71          */
72         private $logger;
73
74         public function __construct(BaseURL $baseURL, IManageConfigValues $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
75         {
76                 $this->hostname = $baseURL->getHostname();
77                 $this->config   = $config;
78                 $this->dba      = $dba;
79                 $this->profiler = $profiler;
80                 $this->logger   = $logger;
81         }
82
83         /**
84          * This method creates a CacheDriver for distributed caching with the given cache driver name
85          *
86          * @param string|null $type The cache type to create (default is per config)
87          *
88          * @return ICanCache  The instance of the CacheDriver
89          *
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
92          */
93         public function createDistributed(string $type = null): ICanCache
94         {
95                 if ($type === Enum\Type::APCU) {
96                         throw new InvalidCacheDriverException('apcu doesn\'t support distributed caching.');
97                 }
98
99                 return $this->create($type ?? $this->config->get('system', 'distributed_cache_driver', self::DEFAULT_TYPE));
100         }
101
102         /**
103          * This method creates a CacheDriver for local caching with the given cache driver name
104          *
105          * @param string|null $type The cache type to create (default is per config)
106          *
107          * @return ICanCache  The instance of the CacheDriver
108          *
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
111          */
112         public function createLocal(string $type = null): ICanCache
113         {
114                 return $this->create($type ?? $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE));
115         }
116
117         /**
118          * Creates a new Cache instance
119          *
120          * @param string $type The type of cache
121          *
122          * @return ICanCache
123          *
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
126          */
127         protected function create(string $type): ICanCache
128         {
129                 switch ($type) {
130                         case Enum\Type::MEMCACHE:
131                                 $cache = new Type\MemcacheCache($this->hostname, $this->config);
132                                 break;
133                         case Enum\Type::MEMCACHED:
134                                 $cache = new Type\MemcachedCache($this->hostname, $this->config, $this->logger);
135                                 break;
136                         case Enum\Type::REDIS:
137                                 $cache = new Type\RedisCache($this->hostname, $this->config);
138                                 break;
139                         case Enum\Type::APCU:
140                                 $cache = new Type\APCuCache($this->hostname);
141                                 break;
142                         default:
143                                 $cache = new Type\DatabaseCache($this->hostname, $this->dba);
144                 }
145
146                 $profiling = $this->config->get('system', 'profiling', false);
147
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);
151                 } else {
152                         return $cache;
153                 }
154         }
155 }