1 | <?php |
||
2 | |||
3 | namespace Doctrine\Common\Cache; |
||
4 | |||
5 | use Traversable; |
||
6 | use function array_values; |
||
7 | use function count; |
||
8 | use function iterator_to_array; |
||
9 | |||
10 | /** |
||
11 | * Cache provider that allows to easily chain multiple cache providers |
||
12 | */ |
||
13 | class ChainCache extends CacheProvider |
||
14 | { |
||
15 | /** @var CacheProvider[] */ |
||
16 | private $cacheProviders = []; |
||
17 | |||
18 | /** @var int */ |
||
19 | private $defaultLifeTimeForDownstreamCacheProviders = 0; |
||
20 | |||
21 | /** |
||
22 | * @param CacheProvider[] $cacheProviders |
||
23 | */ |
||
24 | 88 | public function __construct($cacheProviders = []) |
|
25 | { |
||
26 | 88 | $this->cacheProviders = $cacheProviders instanceof Traversable |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
27 | 1 | ? iterator_to_array($cacheProviders, false) |
|
28 | 87 | : array_values($cacheProviders); |
|
29 | 88 | } |
|
30 | |||
31 | 2 | public function setDefaultLifeTimeForDownstreamCacheProviders(int $defaultLifeTimeForDownstreamCacheProviders) : void |
|
32 | { |
||
33 | 2 | $this->defaultLifeTimeForDownstreamCacheProviders = $defaultLifeTimeForDownstreamCacheProviders; |
|
34 | 2 | } |
|
35 | |||
36 | /** |
||
37 | * {@inheritDoc} |
||
38 | */ |
||
39 | 3 | public function setNamespace($namespace) |
|
40 | { |
||
41 | 3 | parent::setNamespace($namespace); |
|
42 | |||
43 | 3 | foreach ($this->cacheProviders as $cacheProvider) { |
|
44 | 3 | $cacheProvider->setNamespace($namespace); |
|
45 | } |
||
46 | 3 | } |
|
47 | |||
48 | /** |
||
49 | * {@inheritDoc} |
||
50 | */ |
||
51 | 83 | protected function doFetch($id) |
|
52 | { |
||
53 | 83 | foreach ($this->cacheProviders as $key => $cacheProvider) { |
|
54 | 83 | if ($cacheProvider->doContains($id)) { |
|
55 | 65 | $value = $cacheProvider->doFetch($id); |
|
56 | |||
57 | // We populate all the previous cache layers (that are assumed to be faster) |
||
58 | 65 | for ($subKey = $key - 1; $subKey >= 0; $subKey--) { |
|
59 | 3 | $this->cacheProviders[$subKey]->doSave($id, $value, $this->defaultLifeTimeForDownstreamCacheProviders); |
|
60 | } |
||
61 | |||
62 | 83 | return $value; |
|
63 | } |
||
64 | } |
||
65 | |||
66 | 83 | return false; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 5 | protected function doFetchMultiple(array $keys) |
|
73 | { |
||
74 | /** @var CacheProvider[] $traversedProviders */ |
||
75 | 5 | $traversedProviders = []; |
|
76 | 5 | $keysCount = count($keys); |
|
77 | 5 | $fetchedValues = []; |
|
78 | |||
79 | 5 | foreach ($this->cacheProviders as $key => $cacheProvider) { |
|
80 | 5 | $fetchedValues = $cacheProvider->doFetchMultiple($keys); |
|
81 | |||
82 | // We populate all the previous cache layers (that are assumed to be faster) |
||
83 | 5 | if (count($fetchedValues) === $keysCount) { |
|
84 | 5 | foreach ($traversedProviders as $previousCacheProvider) { |
|
85 | 2 | $previousCacheProvider->doSaveMultiple($fetchedValues, $this->defaultLifeTimeForDownstreamCacheProviders); |
|
86 | } |
||
87 | |||
88 | 5 | return $fetchedValues; |
|
89 | } |
||
90 | |||
91 | 3 | $traversedProviders[] = $cacheProvider; |
|
92 | } |
||
93 | |||
94 | 1 | return $fetchedValues; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * {@inheritDoc} |
||
99 | */ |
||
100 | 69 | protected function doContains($id) |
|
101 | { |
||
102 | 69 | foreach ($this->cacheProviders as $cacheProvider) { |
|
103 | 69 | if ($cacheProvider->doContains($id)) { |
|
104 | 69 | return true; |
|
105 | } |
||
106 | } |
||
107 | |||
108 | 50 | return false; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | 73 | protected function doSave($id, $data, $lifeTime = 0) |
|
115 | { |
||
116 | 73 | $stored = true; |
|
117 | |||
118 | 73 | foreach ($this->cacheProviders as $cacheProvider) { |
|
119 | 73 | $stored = $cacheProvider->doSave($id, $data, $lifeTime) && $stored; |
|
120 | } |
||
121 | |||
122 | 73 | return $stored; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 2 | protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) |
|
129 | { |
||
130 | 2 | $stored = true; |
|
131 | |||
132 | 2 | foreach ($this->cacheProviders as $cacheProvider) { |
|
133 | 2 | $stored = $cacheProvider->doSaveMultiple($keysAndValues, $lifetime) && $stored; |
|
134 | } |
||
135 | |||
136 | 2 | return $stored; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * {@inheritDoc} |
||
141 | */ |
||
142 | 45 | protected function doDelete($id) |
|
143 | { |
||
144 | 45 | $deleted = true; |
|
145 | |||
146 | 45 | foreach ($this->cacheProviders as $cacheProvider) { |
|
147 | 45 | $deleted = $cacheProvider->doDelete($id) && $deleted; |
|
148 | } |
||
149 | |||
150 | 45 | return $deleted; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | 2 | protected function doDeleteMultiple(array $keys) |
|
157 | { |
||
158 | 2 | $deleted = true; |
|
159 | |||
160 | 2 | foreach ($this->cacheProviders as $cacheProvider) { |
|
161 | 2 | $deleted = $cacheProvider->doDeleteMultiple($keys) && $deleted; |
|
162 | } |
||
163 | |||
164 | 2 | return $deleted; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * {@inheritDoc} |
||
169 | */ |
||
170 | 3 | protected function doFlush() |
|
171 | { |
||
172 | 3 | $flushed = true; |
|
173 | |||
174 | 3 | foreach ($this->cacheProviders as $cacheProvider) { |
|
175 | 3 | $flushed = $cacheProvider->doFlush() && $flushed; |
|
176 | } |
||
177 | |||
178 | 3 | return $flushed; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * {@inheritDoc} |
||
183 | */ |
||
184 | 1 | protected function doGetStats() |
|
185 | { |
||
186 | // We return all the stats from all adapters |
||
187 | 1 | $stats = []; |
|
188 | |||
189 | 1 | foreach ($this->cacheProviders as $cacheProvider) { |
|
190 | 1 | $stats[] = $cacheProvider->doGetStats(); |
|
191 | } |
||
192 | |||
193 | 1 | return $stats; |
|
194 | } |
||
195 | } |
||
196 |