1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\Persistence\Cache\Adapter; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface; |
12
|
|
|
use Psr\Cache\CacheItemInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Internal proxy adapter invalidating cache items on transaction commits/rollbacks. |
16
|
|
|
*/ |
17
|
|
|
class TransactionalCacheAdapterDecorator implements TransactionAwareAdapterInterface |
18
|
|
|
{ |
19
|
|
|
/** @var \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface */ |
20
|
|
|
protected $innerPool; |
21
|
|
|
|
22
|
|
|
/** @var int */ |
23
|
|
|
protected $transactionDepth; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
protected $deferredTagsInvalidation; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
protected $deferredItemsDeletion; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface $innerPool |
33
|
|
|
* @param int $transactionDepth |
34
|
|
|
* @param array $deferredTagsInvalidation |
35
|
|
|
* @param array $deferredItemsDeletion |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
TagAwareAdapterInterface $innerPool, |
39
|
|
|
int $transactionDepth = 0, |
40
|
|
|
array $deferredTagsInvalidation = [], |
41
|
|
|
array $deferredItemsDeletion = [] |
42
|
|
|
) { |
43
|
|
|
$this->innerPool = $innerPool; |
44
|
|
|
$this->transactionDepth = $transactionDepth; |
45
|
|
|
$this->deferredTagsInvalidation = $deferredTagsInvalidation; |
46
|
|
|
$this->deferredItemsDeletion = $deferredItemsDeletion; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function getItem($key) |
53
|
|
|
{ |
54
|
|
|
return $this->innerPool->getItem($key); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getItems(array $keys = []) |
61
|
|
|
{ |
62
|
|
|
return $this->innerPool->getItems($keys); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function hasItem($key) |
69
|
|
|
{ |
70
|
|
|
return $this->innerPool->hasItem($key); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function deleteItem($key) |
77
|
|
|
{ |
78
|
|
|
if ($this->transactionDepth > 0) { |
79
|
|
|
$this->deferredItemsDeletion[$this->transactionDepth][] = $key; |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->innerPool->deleteItem($key); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function deleteItems(array $keys) |
91
|
|
|
{ |
92
|
|
|
if ($this->transactionDepth > 0) { |
93
|
|
|
$this->deferredItemsDeletion[$this->transactionDepth] = array_merge( |
94
|
|
|
$this->deferredItemsDeletion[$this->transactionDepth], |
95
|
|
|
$keys |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->innerPool->deleteItems($keys); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function invalidateTags(array $tags) |
108
|
|
|
{ |
109
|
|
|
if ($this->transactionDepth > 0) { |
110
|
|
|
$this->deferredTagsInvalidation[$this->transactionDepth] = array_merge( |
111
|
|
|
$this->deferredTagsInvalidation[$this->transactionDepth], |
112
|
|
|
$tags |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->innerPool->invalidateTags($tags); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function clear() |
125
|
|
|
{ |
126
|
|
|
$this->deferredItemsDeletion = []; |
127
|
|
|
$this->deferredTagsInvalidation = []; |
128
|
|
|
$this->transactionDepth = 0; |
129
|
|
|
|
130
|
|
|
return $this->innerPool->clear(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function save(CacheItemInterface $item) |
137
|
|
|
{ |
138
|
|
|
return $this->innerPool->save($item); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function saveDeferred(CacheItemInterface $item) |
145
|
|
|
{ |
146
|
|
|
return $this->innerPool->saveDeferred($item); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function commit() |
153
|
|
|
{ |
154
|
|
|
return $this->innerPool->commit(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function startTransaction(): void |
158
|
|
|
{ |
159
|
|
|
++$this->transactionDepth; |
160
|
|
|
$this->deferredTagsInvalidation[$this->transactionDepth] = []; |
161
|
|
|
$this->deferredItemsDeletion[$this->transactionDepth] = []; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function stopTransaction(): void |
165
|
|
|
{ |
166
|
|
|
if ($this->transactionDepth === 0) { |
167
|
|
|
return; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->invalidateDeferredTags(); |
171
|
|
|
$this->deleteDeferredItems(); |
172
|
|
|
|
173
|
|
|
unset( |
174
|
|
|
$this->deferredItemsDeletion[$this->transactionDepth], |
175
|
|
|
$this->deferredTagsInvalidation[$this->transactionDepth] |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
--$this->transactionDepth; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
protected function invalidateDeferredTags(): void |
182
|
|
|
{ |
183
|
|
|
$tags = $this->deferredTagsInvalidation[$this->transactionDepth]; |
184
|
|
|
|
185
|
|
|
$this->innerPool->invalidateTags(array_unique($tags)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
protected function deleteDeferredItems(): void |
189
|
|
|
{ |
190
|
|
|
$keys = $this->deferredItemsDeletion[$this->transactionDepth]; |
191
|
|
|
|
192
|
|
|
$this->innerPool->deleteItems(array_unique($keys)); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|