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
|
|
|
|
16
|
|
|
namespace phpFastCache\Bundle\Service; |
17
|
|
|
|
18
|
|
|
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
19
|
|
|
use phpFastCache\CacheManager; |
20
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
21
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Cache |
25
|
|
|
* @package phpFastCache\Bundle\Service |
26
|
|
|
*/ |
27
|
|
|
class Cache |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var self |
31
|
|
|
*/ |
32
|
|
|
protected static $selfInstance; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $config = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Stopwatch |
41
|
|
|
*/ |
42
|
|
|
protected $stopwatch; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Contains all cache instances |
46
|
|
|
* |
47
|
|
|
* @var \phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface[] |
48
|
|
|
*/ |
49
|
|
|
protected $cacheInstances = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Cache constructor. |
53
|
|
|
* |
54
|
|
|
* @param array $config |
55
|
|
|
* @param Stopwatch $stopwatch |
56
|
|
|
* |
57
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException |
58
|
|
|
*/ |
59
|
|
|
protected function __construct($config, Stopwatch $stopwatch = null) |
60
|
|
|
{ |
61
|
|
|
$this->config = (array) $config; |
62
|
|
|
$this->stopwatch = $stopwatch; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Factory instance provider |
67
|
|
|
* |
68
|
|
|
* @param array $config |
69
|
|
|
* @param Stopwatch $stopwatch |
70
|
|
|
* |
71
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException |
72
|
|
|
* @return self |
73
|
|
|
*/ |
74
|
|
|
public static function getInstance($config, Stopwatch $stopwatch = null) |
75
|
|
|
{ |
76
|
|
|
return self::$selfInstance ?: self::$selfInstance = new self($config, $stopwatch); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set a new cache instance |
81
|
|
|
* |
82
|
|
|
* @param string $name |
83
|
|
|
* @param ExtendedCacheItemPoolInterface $instance |
84
|
|
|
* |
85
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException |
86
|
|
|
*/ |
87
|
|
|
public function createInstance($name, $instance) |
88
|
|
|
{ |
89
|
|
|
if (array_key_exists($name, $this->cacheInstances) && $this->cacheInstances[ $name ] instanceof ExtendedCacheItemPoolInterface) { |
90
|
|
|
throw new phpFastCacheDriverException("Cache instance '{$name}' already exists"); |
91
|
|
|
} |
92
|
|
|
$this->cacheInstances[ $name ] = $instance; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* get a cache instance |
97
|
|
|
* |
98
|
|
|
* @param string $name Name of configured driver |
99
|
|
|
* |
100
|
|
|
* @return \phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface |
101
|
|
|
* |
102
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheDriverException |
103
|
|
|
*/ |
104
|
|
|
public function get($name) |
105
|
|
|
{ |
106
|
|
|
if ($this->stopwatch) { |
107
|
|
|
$this->stopwatch->start(__METHOD__ . "('{$name}')"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!array_key_exists($name, $this->cacheInstances)) { |
111
|
|
|
if (array_key_exists($name, $this->config[ 'drivers' ])) { |
112
|
|
|
$this->createInstance($name, CacheManager::getInstance($this->config[ 'drivers' ][ $name ][ 'type' ], $this->config[ 'drivers' ][ $name ][ 'parameters' ])); |
113
|
|
|
if (!$this->cacheInstances[ $name ] instanceof ExtendedCacheItemPoolInterface) { |
114
|
|
|
throw new phpFastCacheDriverException("Cache instance '{$name}' does not implements ExtendedCacheItemPoolInterface"); |
115
|
|
|
} |
116
|
|
|
} else { |
117
|
|
|
throw new phpFastCacheDriverException("Cache instance '{$name}' not exists, check your config.yml"); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($this->stopwatch) { |
122
|
|
|
$this->stopwatch->stop(__METHOD__ . "('{$name}')"); |
123
|
|
|
} |
124
|
|
|
return $this->cacheInstances[ $name ]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return \phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface |
129
|
|
|
*/ |
130
|
|
|
public function getTwigCacheInstance() |
131
|
|
|
{ |
132
|
|
|
return $this->get($this->config[ 'twig_driver' ]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Return all cache instances |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function getConfig() |
141
|
|
|
{ |
142
|
|
|
return $this->config; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Return all cache instances |
147
|
|
|
* |
148
|
|
|
* @return \phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface[] |
149
|
|
|
*/ |
150
|
|
|
public function getInstances() |
151
|
|
|
{ |
152
|
|
|
return $this->cacheInstances; |
153
|
|
|
} |
154
|
|
|
} |