1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Cache\Backend;
/**
* Remote synchronized cache
*
* This class created for correct work local caches with multiple web nodes,
* that will be check cache status from remote cache
*/
class RemoteSynchronizedCache extends \Zend_Cache_Backend implements \Zend_Cache_Backend_ExtendedInterface
{
/**
* Local backend cache adapter
*
* @var \Zend_Cache_Backend_ExtendedInterface
*/
private $local;
/**
* Remote backend cache adapter
*
* @var \Zend_Cache_Backend_ExtendedInterface
*/
private $remote;
/**
* Cache invalidation time
*
* @var \Zend_Cache_Backend_ExtendedInterface
*/
protected $cacheInvalidationTime;
/**
* {@inheritdoc}
*/
protected $_options = [
'remote_backend' => '',
'remote_backend_invalidation_time_id' => 'default_remote_backend_invalidation_time',
'remote_backend_custom_naming' => true,
'remote_backend_autoload' => true,
'remote_backend_options' => [],
'local_backend' => '',
'local_backend_options' => [],
'local_backend_custom_naming' => true,
'local_backend_autoload' => true
];
/**
* @param array $options
*/
public function __construct(array $options = [])
{
parent::__construct($options);
$universalOptions = array_diff_key($options, $this->_options);
if ($this->_options['remote_backend'] === null) {
\Zend_Cache::throwException('remote_backend option must be set');
} elseif ($this->_options['remote_backend'] instanceof \Zend_Cache_Backend_ExtendedInterface) {
$this->remote = $this->_options['remote_backend'];
} else {
$this->remote = \Zend_Cache::_makeBackend(
$this->_options['remote_backend'],
array_merge($universalOptions, $this->_options['remote_backend_options']),
$this->_options['remote_backend_custom_naming'],
$this->_options['remote_backend_autoload']
);
if (!($this->remote instanceof \Zend_Cache_Backend_ExtendedInterface)) {
\Zend_Cache::throwException(
'remote_backend must implement the Zend_Cache_Backend_ExtendedInterface interface'
);
}
}
if ($this->_options['local_backend'] === null) {
\Zend_Cache::throwException('local_backend option must be set');
} elseif ($this->_options['local_backend'] instanceof \Zend_Cache_Backend_ExtendedInterface) {
$this->local = $this->_options['local_backend'];
} else {
$this->local = \Zend_Cache::_makeBackend(
$this->_options['local_backend'],
array_merge($universalOptions, $this->_options['local_backend_options']),
$this->_options['local_backend_custom_naming'],
$this->_options['local_backend_autoload']
);
if (!($this->local instanceof \Zend_Cache_Backend_ExtendedInterface)) {
\Zend_Cache::throwException(
'local_backend must implement the Zend_Cache_Backend_ExtendedInterface interface'
);
}
}
}
/**
* Update remote cache status info
*
* @return void
*/
private function updateRemoteCacheStatusInfo()
{
$this->remote->save(time(), $this->_options['remote_backend_invalidation_time_id'], [], null);
$this->cacheInvalidationTime = null;
}
/**
* {@inheritdoc}
*/
public function setDirectives($directives)
{
return $this->local->setDirectives($directives);
}
/**
* {@inheritdoc}
*/
public function load($id, $doNotTestCacheValidity = false)
{
$dataModificationTime = $this->local->test($id);
if ($this->cacheInvalidationTime === null) {
$this->cacheInvalidationTime = $this->remote->load($this->_options['remote_backend_invalidation_time_id']);
}
if ($dataModificationTime >= $this->cacheInvalidationTime) {
return $this->local->load($id, $doNotTestCacheValidity);
} else {
return false;
}
}
/**
* {@inheritdoc}
*/
public function test($id)
{
return $this->local->test($id);
}
/**
* {@inheritdoc}
*/
public function save($data, $id, $tags = [], $specificLifetime = false)
{
return $this->local->save($data, $id, $tags, $specificLifetime);
}
/**
* {@inheritdoc}
*/
public function remove($id)
{
$this->updateRemoteCacheStatusInfo();
return $this->local->remove($id);
}
/**
* {@inheritdoc}
*/
public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = [])
{
$this->updateRemoteCacheStatusInfo();
return $this->local->clean($mode, $tags);
}
/**
* {@inheritdoc}
*/
public function getIds()
{
return $this->local->getIds();
}
/**
* {@inheritdoc}
*/
public function getTags()
{
return $this->local->getTags();
}
/**
* {@inheritdoc}
*/
public function getIdsMatchingTags($tags = [])
{
return $this->local->getIdsMatchingTags($tags);
}
/**
* {@inheritdoc}
*/
public function getIdsNotMatchingTags($tags = [])
{
return $this->local->getIdsNotMatchingTags($tags);
}
/**
* {@inheritdoc}
*/
public function getIdsMatchingAnyTags($tags = [])
{
return $this->local->getIdsMatchingAnyTags($tags);
}
/**
* {@inheritdoc}
*/
public function getFillingPercentage()
{
return $this->local->getFillingPercentage();
}
/**
* {@inheritdoc}
*/
public function getMetadatas($id)
{
return $this->local->getMetadatas($id);
}
/**
* {@inheritdoc}
*/
public function touch($id, $extraLifetime)
{
return $this->local->touch($id, $extraLifetime);
}
/**
* {@inheritdoc}
*/
public function getCapabilities()
{
return $this->local->getCapabilities();
}
}