|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of phpFastCache. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
|
11
|
|
|
* @author PastisD https://github.com/PastisD |
|
12
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
declare(strict_types=1); |
|
16
|
|
|
|
|
17
|
|
|
namespace Phpfastcache\Bundle\Service; |
|
18
|
|
|
|
|
19
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
|
20
|
|
|
use Phpfastcache\CacheManager; |
|
21
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverException; |
|
22
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException; |
|
23
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class Cache |
|
27
|
|
|
* @package Phpfastcache\Bundle\Service |
|
28
|
|
|
*/ |
|
29
|
|
|
class Phpfastcache |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $config = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Stopwatch |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $stopwatch; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Contains all cache instances |
|
43
|
|
|
* |
|
44
|
|
|
* @var \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface[] |
|
45
|
|
|
*/ |
|
46
|
|
|
private $cacheInstances = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Cache constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $config |
|
52
|
|
|
* @param Stopwatch $stopwatch |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \Phpfastcache\Exceptions\phpFastCacheDriverException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(array $config, Stopwatch $stopwatch = null) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->config = $config; |
|
59
|
|
|
$this->stopwatch = $stopwatch; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set a new cache instance |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $name |
|
66
|
|
|
* @param ExtendedCacheItemPoolInterface $instance |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \Phpfastcache\Exceptions\phpFastCacheDriverException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function createInstance(string $name, ExtendedCacheItemPoolInterface $instance) |
|
71
|
|
|
{ |
|
72
|
|
|
if (\array_key_exists($name, $this->cacheInstances) && $this->cacheInstances[ $name ] instanceof ExtendedCacheItemPoolInterface) { |
|
73
|
|
|
throw new PhpfastcacheDriverException("Cache instance '{$name}' already exists"); |
|
74
|
|
|
} |
|
75
|
|
|
$this->cacheInstances[ $name ] = $instance; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* get a cache instance |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $name Name of configured driver |
|
82
|
|
|
* |
|
83
|
|
|
* @return ExtendedCacheItemPoolInterface |
|
84
|
|
|
* |
|
85
|
|
|
* @throws \Phpfastcache\Exceptions\phpFastCacheDriverException |
|
86
|
|
|
* @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException |
|
87
|
|
|
*/ |
|
88
|
|
|
public function get(string $name): ExtendedCacheItemPoolInterface |
|
89
|
|
|
{ |
|
90
|
|
|
if ($this->stopwatch) { |
|
91
|
|
|
$this->stopwatch->start(__METHOD__ . "('{$name}')"); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (!\array_key_exists($name, $this->cacheInstances)) { |
|
95
|
|
|
if (\array_key_exists($name, $this->config[ 'drivers' ])) { |
|
96
|
|
|
$driverClass = CacheManager::getDriverClass($this->config[ 'drivers' ][ $name ][ 'type' ]); |
|
97
|
|
|
if (\is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)){ |
|
98
|
|
|
$configClass = $driverClass::getConfigClass(); |
|
99
|
|
|
if(\class_exists($configClass)){ |
|
100
|
|
|
$this->createInstance( |
|
101
|
|
|
$name, |
|
102
|
|
|
CacheManager::getInstance( |
|
103
|
|
|
$this->config[ 'drivers' ][ $name ][ 'type' ], |
|
104
|
|
|
new $configClass($this->config[ 'drivers' ][ $name ][ 'parameters' ]) |
|
105
|
|
|
) |
|
106
|
|
|
); |
|
107
|
|
|
}else{ |
|
108
|
|
|
throw new PhpfastcacheInvalidConfigurationException('Invalid configuration class name: ' . $configClass); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (!isset($this->cacheInstances[ $name ]) || !($this->cacheInstances[ $name ] instanceof ExtendedCacheItemPoolInterface)) { |
|
113
|
|
|
throw new PhpfastcacheDriverException("Cache instance '{$name}' does not implements ExtendedCacheItemPoolInterface"); |
|
114
|
|
|
} |
|
115
|
|
|
} else { |
|
116
|
|
|
throw new PhpfastcacheDriverException("Cache instance '{$name}' not exists, check your config.yml"); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ($this->stopwatch) { |
|
121
|
|
|
$this->stopwatch->stop(__METHOD__ . "('{$name}')"); |
|
122
|
|
|
} |
|
123
|
|
|
return $this->cacheInstances[ $name ]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getTwigCacheInstance(): ExtendedCacheItemPoolInterface |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->get($this->config[ 'twig_driver' ]); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Return all cache instances |
|
136
|
|
|
* |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getConfig(): array |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->config; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Return all cache instances |
|
146
|
|
|
* |
|
147
|
|
|
* @return \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface[] |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getInstances(): array |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->cacheInstances; |
|
152
|
|
|
} |
|
153
|
|
|
} |