]> git.mxchange.org Git - friendica.git/blob - src/Factory/CacheFactory.php
Create constants for Mastodon notification types
[friendica.git] / src / Factory / CacheFactory.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\Factory;
23
24 use Friendica\App\BaseURL;
25 use Friendica\Core\Cache;
26 use Friendica\Core\Cache\ICache;
27 use Friendica\Core\Config\IConfig;
28 use Friendica\Database\Database;
29 use Friendica\Util\Profiler;
30 use Psr\Log\LoggerInterface;
31
32 /**
33  * Class CacheFactory
34  *
35  * @package Friendica\Core\Cache
36  *
37  * A basic class to generate a CacheDriver
38  */
39 class CacheFactory
40 {
41         /**
42          * @var string The default cache if nothing set
43          */
44         const DEFAULT_TYPE = Cache\Type::DATABASE;
45
46         /**
47          * @var IConfig The IConfiguration to read parameters out of the config
48          */
49         private $config;
50
51         /**
52          * @var Database The database connection in case that the cache is used the dba connection
53          */
54         private $dba;
55
56         /**
57          * @var string The hostname, used as Prefix for Caching
58          */
59         private $hostname;
60
61         /**
62          * @var Profiler The optional profiler if the cached should be profiled
63          */
64         private $profiler;
65
66         /**
67          * @var LoggerInterface The Friendica Logger
68          */
69         private $logger;
70
71         public function __construct(BaseURL $baseURL, IConfig $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
72         {
73                 $this->hostname = $baseURL->getHostname();
74                 $this->config   = $config;
75                 $this->dba      = $dba;
76                 $this->profiler = $profiler;
77                 $this->logger   = $logger;
78         }
79
80         /**
81          * This method creates a CacheDriver for the given cache driver name
82          *
83          * @param string $type The cache type to create (default is per config)
84          *
85          * @return ICache  The instance of the CacheDriver
86          * @throws \Exception    The exception if something went wrong during the CacheDriver creation
87          */
88         public function create(string $type = null)
89         {
90                 if (empty($type)) {
91                         $type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
92                 }
93
94                 switch ($type) {
95                         case Cache\Type::MEMCACHE:
96                                 $cache = new Cache\MemcacheCache($this->hostname, $this->config);
97                                 break;
98                         case Cache\Type::MEMCACHED:
99                                 $cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger);
100                                 break;
101                         case Cache\Type::REDIS:
102                                 $cache = new Cache\RedisCache($this->hostname, $this->config);
103                                 break;
104                         case Cache\Type::APCU:
105                                 $cache = new Cache\APCuCache($this->hostname);
106                                 break;
107                         default:
108                                 $cache = new Cache\DatabaseCache($this->hostname, $this->dba);
109                 }
110
111                 $profiling = $this->config->get('system', 'profiling', false);
112
113                 // In case profiling is enabled, wrap the ProfilerCache around the current cache
114                 if (isset($profiling) && $profiling !== false) {
115                         return new Cache\ProfilerCache($cache, $this->profiler);
116                 } else {
117                         return $cache;
118                 }
119         }
120 }