RemoteSynchronizedCache.php 6.2 KB

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