|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Framework\Cache\Backend; |
| 8 | + |
| 9 | +/** |
| 10 | + * Remote synchronized cache |
| 11 | + * |
| 12 | + * This class created for correct work local caches with multiple web nodes, |
| 13 | + * that will be check cache status from remote cache |
| 14 | + */ |
| 15 | +class RemoteSynchronizedCache extends \Zend_Cache_Backend implements \Zend_Cache_Backend_ExtendedInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Local backend cache adapter |
| 19 | + * |
| 20 | + * @var \Zend_Cache_Backend_ExtendedInterface |
| 21 | + */ |
| 22 | + private $local; |
| 23 | + |
| 24 | + /** |
| 25 | + * Remote backend cache adapter |
| 26 | + * |
| 27 | + * @var \Zend_Cache_Backend_ExtendedInterface |
| 28 | + */ |
| 29 | + private $remote; |
| 30 | + |
| 31 | + /** |
| 32 | + * Cache invalidation time |
| 33 | + * |
| 34 | + * @var \Zend_Cache_Backend_ExtendedInterface |
| 35 | + */ |
| 36 | + protected $cacheInvalidationTime; |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritdoc} |
| 40 | + */ |
| 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 | + |
| 53 | + /** |
| 54 | + * @param array $options |
| 55 | + */ |
| 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)) { |
| 74 | + \Zend_Cache::throwException( |
| 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)) { |
| 92 | + \Zend_Cache::throwException( |
| 93 | + 'local_backend must implement the Zend_Cache_Backend_ExtendedInterface interface' |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Update remote cache status info |
| 101 | + * |
| 102 | + * @return void |
| 103 | + */ |
| 104 | + private function updateRemoteCacheStatusInfo() |
| 105 | + { |
| 106 | + $this->remote->save(time(), $this->_options['remote_backend_invalidation_time_id'], [], null); |
| 107 | + $this->cacheInvalidationTime = null; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * {@inheritdoc} |
| 112 | + */ |
| 113 | + public function setDirectives($directives) |
| 114 | + { |
| 115 | + return $this->local->setDirectives($directives); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * {@inheritdoc} |
| 120 | + */ |
| 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 | + |
| 134 | + /** |
| 135 | + * {@inheritdoc} |
| 136 | + */ |
| 137 | + public function test($id) |
| 138 | + { |
| 139 | + return $this->local->test($id); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * {@inheritdoc} |
| 144 | + */ |
| 145 | + public function save($data, $id, $tags = [], $specificLifetime = false) |
| 146 | + { |
| 147 | + return $this->local->save($data, $id, $tags, $specificLifetime); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * {@inheritdoc} |
| 152 | + */ |
| 153 | + public function remove($id) |
| 154 | + { |
| 155 | + $this->updateRemoteCacheStatusInfo(); |
| 156 | + return $this->local->remove($id); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * {@inheritdoc} |
| 161 | + */ |
| 162 | + public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = []) |
| 163 | + { |
| 164 | + $this->updateRemoteCacheStatusInfo(); |
| 165 | + return $this->local->clean($mode, $tags); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * {@inheritdoc} |
| 170 | + */ |
| 171 | + public function getIds() |
| 172 | + { |
| 173 | + return $this->local->getIds(); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * {@inheritdoc} |
| 178 | + */ |
| 179 | + public function getTags() |
| 180 | + { |
| 181 | + return $this->local->getTags(); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * {@inheritdoc} |
| 186 | + */ |
| 187 | + public function getIdsMatchingTags($tags = []) |
| 188 | + { |
| 189 | + return $this->local->getIdsMatchingTags($tags); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * {@inheritdoc} |
| 194 | + */ |
| 195 | + public function getIdsNotMatchingTags($tags = []) |
| 196 | + { |
| 197 | + return $this->local->getIdsNotMatchingTags($tags); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * {@inheritdoc} |
| 202 | + */ |
| 203 | + public function getIdsMatchingAnyTags($tags = []) |
| 204 | + { |
| 205 | + return $this->local->getIdsMatchingAnyTags($tags); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * {@inheritdoc} |
| 210 | + */ |
| 211 | + public function getFillingPercentage() |
| 212 | + { |
| 213 | + return $this->local->getFillingPercentage(); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * {@inheritdoc} |
| 218 | + */ |
| 219 | + public function getMetadatas($id) |
| 220 | + { |
| 221 | + return $this->local->getMetadatas($id); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * {@inheritdoc} |
| 226 | + */ |
| 227 | + public function touch($id, $extraLifetime) |
| 228 | + { |
| 229 | + return $this->local->touch($id, $extraLifetime); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * {@inheritdoc} |
| 234 | + */ |
| 235 | + public function getCapabilities() |
| 236 | + { |
| 237 | + return $this->local->getCapabilities(); |
| 238 | + } |
| 239 | +} |
0 commit comments