Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RemoteSynchronizedCache.php
Go to the documentation of this file.
1 <?php
8 
16 {
22  private $local;
23 
29  private $remote;
30 
37 
41  protected $_options = [
42  'remote_backend' => '',
43  'remote_backend_invalidation_time_id' => 'default_remote_backend_invalidation_time',
44  'remote_backend_custom_naming' => true,
45  'remote_backend_autoload' => true,
46  'remote_backend_options' => [],
47  'local_backend' => '',
48  'local_backend_options' => [],
49  'local_backend_custom_naming' => true,
50  'local_backend_autoload' => true
51  ];
52 
56  public function __construct(array $options = [])
57  {
58  parent::__construct($options);
59 
60  $universalOptions = array_diff_key($options, $this->_options);
61 
62  if ($this->_options['remote_backend'] === null) {
63  \Zend_Cache::throwException('remote_backend option must be set');
64  } elseif ($this->_options['remote_backend'] instanceof \Zend_Cache_Backend_ExtendedInterface) {
65  $this->remote = $this->_options['remote_backend'];
66  } else {
67  $this->remote = \Zend_Cache::_makeBackend(
68  $this->_options['remote_backend'],
69  array_merge($universalOptions, $this->_options['remote_backend_options']),
70  $this->_options['remote_backend_custom_naming'],
71  $this->_options['remote_backend_autoload']
72  );
73  if (!($this->remote instanceof \Zend_Cache_Backend_ExtendedInterface)) {
75  'remote_backend must implement the Zend_Cache_Backend_ExtendedInterface interface'
76  );
77  }
78  }
79 
80  if ($this->_options['local_backend'] === null) {
81  \Zend_Cache::throwException('local_backend option must be set');
82  } elseif ($this->_options['local_backend'] instanceof \Zend_Cache_Backend_ExtendedInterface) {
83  $this->local = $this->_options['local_backend'];
84  } else {
85  $this->local = \Zend_Cache::_makeBackend(
86  $this->_options['local_backend'],
87  array_merge($universalOptions, $this->_options['local_backend_options']),
88  $this->_options['local_backend_custom_naming'],
89  $this->_options['local_backend_autoload']
90  );
91  if (!($this->local instanceof \Zend_Cache_Backend_ExtendedInterface)) {
93  'local_backend must implement the Zend_Cache_Backend_ExtendedInterface interface'
94  );
95  }
96  }
97  }
98 
104  private function updateRemoteCacheStatusInfo()
105  {
106  $this->remote->save(time(), $this->_options['remote_backend_invalidation_time_id'], [], null);
107  $this->cacheInvalidationTime = null;
108  }
109 
113  public function setDirectives($directives)
114  {
115  return $this->local->setDirectives($directives);
116  }
117 
121  public function load($id, $doNotTestCacheValidity = false)
122  {
123  $dataModificationTime = $this->local->test($id);
124  if ($this->cacheInvalidationTime === null) {
125  $this->cacheInvalidationTime = $this->remote->load($this->_options['remote_backend_invalidation_time_id']);
126  }
127  if ($dataModificationTime >= $this->cacheInvalidationTime) {
128  return $this->local->load($id, $doNotTestCacheValidity);
129  } else {
130  return false;
131  }
132  }
133 
137  public function test($id)
138  {
139  return $this->local->test($id);
140  }
141 
145  public function save($data, $id, $tags = [], $specificLifetime = false)
146  {
147  return $this->local->save($data, $id, $tags, $specificLifetime);
148  }
149 
153  public function remove($id)
154  {
155  $this->updateRemoteCacheStatusInfo();
156  return $this->local->remove($id);
157  }
158 
162  public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = [])
163  {
164  $this->updateRemoteCacheStatusInfo();
165  return $this->local->clean($mode, $tags);
166  }
167 
171  public function getIds()
172  {
173  return $this->local->getIds();
174  }
175 
179  public function getTags()
180  {
181  return $this->local->getTags();
182  }
183 
187  public function getIdsMatchingTags($tags = [])
188  {
189  return $this->local->getIdsMatchingTags($tags);
190  }
191 
195  public function getIdsNotMatchingTags($tags = [])
196  {
197  return $this->local->getIdsNotMatchingTags($tags);
198  }
199 
203  public function getIdsMatchingAnyTags($tags = [])
204  {
205  return $this->local->getIdsMatchingAnyTags($tags);
206  }
207 
211  public function getFillingPercentage()
212  {
213  return $this->local->getFillingPercentage();
214  }
215 
219  public function getMetadatas($id)
220  {
221  return $this->local->getMetadatas($id);
222  }
223 
227  public function touch($id, $extraLifetime)
228  {
229  return $this->local->touch($id, $extraLifetime);
230  }
231 
235  public function getCapabilities()
236  {
237  return $this->local->getCapabilities();
238  }
239 }
save($data, $id, $tags=[], $specificLifetime=false)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
clean($mode=\Zend_Cache::CLEANING_MODE_ALL, $tags=[])
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
const CLEANING_MODE_ALL
Definition: Cache.php:72
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
static _makeBackend($backend, $backendOptions, $customBackendNaming=false, $autoload=false)
Definition: Cache.php:124