]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Factory/Cache.php
Merge pull request #10918 from nupplaphil/feat/core_new_paradigm
[friendica.git] / src / Core / Cache / Factory / Cache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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 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 create(string $type = null): ICanCache
94         {
95                 if (empty($type)) {
96                         $type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
97                 }
98
99                 switch ($type) {
100                         case Enum\Type::MEMCACHE:
101                                 $cache = new Type\MemcacheCache($this->hostname, $this->config);
102                                 break;
103                         case Enum\Type::MEMCACHED:
104                                 $cache = new Type\MemcachedCache($this->hostname, $this->config, $this->logger);
105                                 break;
106                         case Enum\Type::REDIS:
107                                 $cache = new Type\RedisCache($this->hostname, $this->config);
108                                 break;
109                         case Enum\Type::APCU:
110                                 $cache = new Type\APCuCache($this->hostname);
111                                 break;
112                         default:
113                                 $cache = new Type\DatabaseCache($this->hostname, $this->dba);
114                 }
115
116                 $profiling = $this->config->get('system', 'profiling', false);
117
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);
121                 } else {
122                         return $cache;
123                 }
124         }
125 }