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