Cache.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * System cache model
  8. * support id and tags prefix support,
  9. */
  10. namespace Magento\Framework\App;
  11. class Cache implements CacheInterface
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected $_frontendIdentifier = \Magento\Framework\App\Cache\Frontend\Pool::DEFAULT_FRONTEND_ID;
  17. /**
  18. * @var \Magento\Framework\App\Cache\Frontend\Pool
  19. */
  20. protected $_frontendPool;
  21. /**
  22. * Cache frontend API
  23. *
  24. * @var \Magento\Framework\Cache\FrontendInterface
  25. */
  26. protected $_frontend;
  27. /**
  28. * @param \Magento\Framework\App\Cache\Frontend\Pool $frontendPool
  29. */
  30. public function __construct(\Magento\Framework\App\Cache\Frontend\Pool $frontendPool)
  31. {
  32. $this->_frontendPool = $frontendPool;
  33. $this->_frontend = $frontendPool->get($this->_frontendIdentifier);
  34. }
  35. /**
  36. * Get cache frontend API object
  37. *
  38. * @return \Magento\Framework\Cache\FrontendInterface
  39. */
  40. public function getFrontend()
  41. {
  42. return $this->_frontend;
  43. }
  44. /**
  45. * Load data from cache by id
  46. *
  47. * @param string $identifier
  48. * @return string
  49. */
  50. public function load($identifier)
  51. {
  52. return $this->_frontend->load($identifier);
  53. }
  54. /**
  55. * Save data
  56. *
  57. * @param string $data
  58. * @param string $identifier
  59. * @param array $tags
  60. * @param int $lifeTime
  61. * @return bool
  62. */
  63. public function save($data, $identifier, $tags = [], $lifeTime = null)
  64. {
  65. return $this->_frontend->save((string)$data, $identifier, $tags, $lifeTime);
  66. }
  67. /**
  68. * Remove cached data by identifier
  69. *
  70. * @param string $identifier
  71. * @return bool
  72. */
  73. public function remove($identifier)
  74. {
  75. return $this->_frontend->remove($identifier);
  76. }
  77. /**
  78. * Clean cached data by specific tag
  79. *
  80. * @param array $tags
  81. * @return bool
  82. */
  83. public function clean($tags = [])
  84. {
  85. if ($tags) {
  86. $result = $this->_frontend->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, (array)$tags);
  87. } else {
  88. /** @deprecated special case of cleaning by empty tags is deprecated after 2.0.0.0-dev42 */
  89. $result = false;
  90. /** @var $cacheFrontend \Magento\Framework\Cache\FrontendInterface */
  91. foreach ($this->_frontendPool as $cacheFrontend) {
  92. if ($cacheFrontend->clean()) {
  93. $result = true;
  94. }
  95. }
  96. }
  97. return $result;
  98. }
  99. }