ConfigCache.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Object manager configuration cache
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Serialize\SerializerInterface;
  10. use Magento\Framework\Serialize\Serializer\Serialize;
  11. class ConfigCache implements \Magento\Framework\ObjectManager\ConfigCacheInterface
  12. {
  13. /**
  14. * @var \Magento\Framework\Cache\FrontendInterface
  15. */
  16. protected $_cacheFrontend;
  17. /**
  18. * Cache prefix
  19. *
  20. * @var string
  21. */
  22. protected $_prefix = 'diConfig';
  23. /**
  24. * @var SerializerInterface
  25. */
  26. private $serializer;
  27. /**
  28. * @param \Magento\Framework\Cache\FrontendInterface $cacheFrontend
  29. */
  30. public function __construct(\Magento\Framework\Cache\FrontendInterface $cacheFrontend)
  31. {
  32. $this->_cacheFrontend = $cacheFrontend;
  33. }
  34. /**
  35. * Retrieve configuration from cache
  36. *
  37. * @param string $key
  38. * @return array|false
  39. */
  40. public function get($key)
  41. {
  42. $data = $this->_cacheFrontend->load($this->_prefix . $key);
  43. if (!$data) {
  44. return false;
  45. }
  46. return $this->getSerializer()->unserialize($data);
  47. }
  48. /**
  49. * Save config to cache
  50. *
  51. * @param array $config
  52. * @param string $key
  53. * @return void
  54. */
  55. public function save(array $config, $key)
  56. {
  57. $this->_cacheFrontend->save($this->getSerializer()->serialize($config), $this->_prefix . $key);
  58. }
  59. /**
  60. * Get serializer
  61. *
  62. * @return SerializerInterface
  63. * @deprecated 101.0.0
  64. */
  65. private function getSerializer()
  66. {
  67. if (null === $this->serializer) {
  68. $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()->get(Serialize::class);
  69. }
  70. return $this->serializer;
  71. }
  72. }