Zend.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Cache\Frontend\Adapter;
  7. /**
  8. * Adapter for Magento -> Zend cache frontend interfaces
  9. */
  10. class Zend implements \Magento\Framework\Cache\FrontendInterface
  11. {
  12. /**
  13. * @var \Zend_Cache_Core
  14. */
  15. protected $_frontend;
  16. /**
  17. * Factory that creates the \Zend_Cache_Cores
  18. *
  19. * @var \Closure
  20. */
  21. private $frontendFactory;
  22. /**
  23. * The pid that owns the $_frontend object
  24. *
  25. * @var int
  26. */
  27. private $pid;
  28. /**
  29. * @param \Closure $frontendFactory
  30. */
  31. public function __construct(\Closure $frontendFactory)
  32. {
  33. $this->frontendFactory = $frontendFactory;
  34. $this->_frontend = $frontendFactory();
  35. $this->pid = getmypid();
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function test($identifier)
  41. {
  42. return $this->getFrontEnd()->test($this->_unifyId($identifier));
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function load($identifier)
  48. {
  49. return $this->getFrontEnd()->load($this->_unifyId($identifier));
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function save($data, $identifier, array $tags = [], $lifeTime = null)
  55. {
  56. return $this->getFrontEnd()->save($data, $this->_unifyId($identifier), $this->_unifyIds($tags), $lifeTime);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function remove($identifier)
  62. {
  63. return $this->getFrontEnd()->remove($this->_unifyId($identifier));
  64. }
  65. /**
  66. * {@inheritdoc}
  67. *
  68. * @throws \InvalidArgumentException Exception is thrown when non-supported cleaning mode is specified
  69. * @throws \Zend_Cache_Exception
  70. */
  71. public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = [])
  72. {
  73. // Cleaning modes 'old' and 'notMatchingTag' are prohibited as a trade off for decoration reliability
  74. if (!in_array(
  75. $mode,
  76. [
  77. \Zend_Cache::CLEANING_MODE_ALL,
  78. \Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  79. \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG
  80. ]
  81. )
  82. ) {
  83. throw new \InvalidArgumentException(
  84. "Magento cache frontend does not support the cleaning mode '{$mode}'."
  85. );
  86. }
  87. return $this->getFrontEnd()->clean($mode, $this->_unifyIds($tags));
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getBackend()
  93. {
  94. return $this->getFrontEnd()->getBackend();
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getLowLevelFrontend()
  100. {
  101. return $this->getFrontEnd();
  102. }
  103. /**
  104. * Retrieve single unified identifier
  105. *
  106. * @param string $identifier
  107. * @return string
  108. */
  109. protected function _unifyId($identifier)
  110. {
  111. return strtoupper($identifier);
  112. }
  113. /**
  114. * Retrieve multiple unified identifiers
  115. *
  116. * @param array $ids
  117. * @return array
  118. */
  119. protected function _unifyIds(array $ids)
  120. {
  121. foreach ($ids as $key => $value) {
  122. $ids[$key] = $this->_unifyId($value);
  123. }
  124. return $ids;
  125. }
  126. /**
  127. * Get frontEnd cache adapter for current pid
  128. *
  129. * @return \Zend_Cache_Core
  130. */
  131. private function getFrontEnd()
  132. {
  133. if (getmypid() === $this->pid) {
  134. return $this->_frontend;
  135. }
  136. $frontendFactory = $this->frontendFactory;
  137. $this->_frontend = $frontendFactory();
  138. $this->pid = getmypid();
  139. return $this->_frontend;
  140. }
  141. }