Cache.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Marketplace\Helper;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. /**
  10. * Cache helper
  11. */
  12. class Cache extends \Magento\Framework\App\Helper\AbstractHelper
  13. {
  14. /**
  15. * @var string
  16. */
  17. protected $pathToCacheFile = 'partners';
  18. /**
  19. * Configuration cache model
  20. *
  21. * @var \Magento\Framework\Config\CacheInterface
  22. */
  23. protected $cache;
  24. /**
  25. * @var SerializerInterface
  26. */
  27. private $serializer;
  28. /**
  29. * @param \Magento\Framework\App\Helper\Context $context
  30. * @param \Magento\Framework\Config\CacheInterface $cache
  31. * @param SerializerInterface $serializer
  32. */
  33. public function __construct(
  34. \Magento\Framework\App\Helper\Context $context,
  35. \Magento\Framework\Config\CacheInterface $cache,
  36. SerializerInterface $serializer = null
  37. ) {
  38. $this->cache = $cache;
  39. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  40. parent::__construct($context);
  41. }
  42. /**
  43. * Load partners from cache
  44. *
  45. * @return array
  46. */
  47. public function loadPartnersFromCache()
  48. {
  49. $data = $this->getCache()->load($this->pathToCacheFile);
  50. if (false !== $data) {
  51. $data = $this->serializer->unserialize($data);
  52. }
  53. return $data;
  54. }
  55. /**
  56. * Save composer packages available for update to cache
  57. *
  58. * @param string $partners
  59. * @return bool
  60. */
  61. public function savePartnersToCache($partners)
  62. {
  63. return $this->getCache()->save($this->serializer->serialize($partners), $this->pathToCacheFile);
  64. }
  65. /**
  66. * @return \Magento\Framework\Config\CacheInterface
  67. */
  68. public function getCache()
  69. {
  70. return $this->cache;
  71. }
  72. }