]> git.mxchange.org Git - friendica.git/blob - src/Factory/CacheFactory.php
Merge pull request #7988 from friendica/MrPetovan-notice
[friendica.git] / src / Factory / CacheFactory.php
1 <?php
2
3 namespace Friendica\Factory;
4
5 use Friendica\App\BaseURL;
6 use Friendica\Core\Cache;
7 use Friendica\Core\Cache\ICache;
8 use Friendica\Core\Config\Configuration;
9 use Friendica\Database\Database;
10 use Friendica\Util\Profiler;
11 use Psr\Log\LoggerInterface;
12
13 /**
14  * Class CacheFactory
15  *
16  * @package Friendica\Core\Cache
17  *
18  * A basic class to generate a CacheDriver
19  */
20 class CacheFactory
21 {
22         /**
23          * @var string The default cache if nothing set
24          */
25         const DEFAULT_TYPE = Cache\Cache::TYPE_DATABASE;
26
27         /**
28          * @var Configuration The configuration to read parameters out of the config
29          */
30         private $config;
31
32         /**
33          * @var Database The database connection in case that the cache is used the dba connection
34          */
35         private $dba;
36
37         /**
38          * @var string The hostname, used as Prefix for Caching
39          */
40         private $hostname;
41
42         /**
43          * @var Profiler The optional profiler if the cached should be profiled
44          */
45         private $profiler;
46
47         /**
48          * @var LoggerInterface The Friendica Logger
49          */
50         private $logger;
51
52         public function __construct(BaseURL $baseURL, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
53         {
54                 $this->hostname = $baseURL->getHostname();
55                 $this->config   = $config;
56                 $this->dba      = $dba;
57                 $this->profiler = $profiler;
58                 $this->logger   = $logger;
59         }
60
61         /**
62          * This method creates a CacheDriver for the given cache driver name
63          *
64          * @param string $type The cache type to create (default is per config)
65          *
66          * @return ICache  The instance of the CacheDriver
67          * @throws \Exception    The exception if something went wrong during the CacheDriver creation
68          */
69         public function create(string $type = null)
70         {
71                 if (empty($type)) {
72                         $type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
73                 }
74
75                 switch ($type) {
76                         case Cache\Cache::TYPE_MEMCACHE:
77                                 $cache = new Cache\MemcacheCache($this->hostname, $this->config);
78                                 break;
79                         case Cache\Cache::TYPE_MEMCACHED:
80                                 $cache = new Cache\MemcachedCache($this->hostname, $this->config, $this->logger);
81                                 break;
82                         case Cache\Cache::TYPE_REDIS:
83                                 $cache = new Cache\RedisCache($this->hostname, $this->config);
84                                 break;
85                         case Cache\Cache::TYPE_APCU:
86                                 $cache = new Cache\APCuCache($this->hostname);
87                                 break;
88                         default:
89                                 $cache = new Cache\DatabaseCache($this->hostname, $this->dba);
90                 }
91
92                 $profiling = $this->config->get('system', 'profiling', false);
93
94                 // In case profiling is enabled, wrap the ProfilerCache around the current cache
95                 if (isset($profiling) && $profiling !== false) {
96                         return new Cache\ProfilerCache($cache, $this->profiler);
97                 } else {
98                         return $cache;
99                 }
100         }
101 }