ArrayAdapter.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Adapter;
  11. use Psr\Cache\CacheItemInterface;
  12. use Psr\Log\LoggerAwareInterface;
  13. use Symfony\Component\Cache\CacheItem;
  14. use Symfony\Component\Cache\ResettableInterface;
  15. use Symfony\Component\Cache\Traits\ArrayTrait;
  16. /**
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. class ArrayAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface
  20. {
  21. use ArrayTrait;
  22. private $createCacheItem;
  23. /**
  24. * @param int $defaultLifetime
  25. * @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
  26. */
  27. public function __construct(int $defaultLifetime = 0, bool $storeSerialized = true)
  28. {
  29. $this->storeSerialized = $storeSerialized;
  30. $this->createCacheItem = \Closure::bind(
  31. function ($key, $value, $isHit) use ($defaultLifetime) {
  32. $item = new CacheItem();
  33. $item->key = $key;
  34. $item->value = $value;
  35. $item->isHit = $isHit;
  36. $item->defaultLifetime = $defaultLifetime;
  37. return $item;
  38. },
  39. null,
  40. CacheItem::class
  41. );
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getItem($key)
  47. {
  48. $isHit = $this->hasItem($key);
  49. try {
  50. if (!$isHit) {
  51. $this->values[$key] = $value = null;
  52. } elseif (!$this->storeSerialized) {
  53. $value = $this->values[$key];
  54. } elseif ('b:0;' === $value = $this->values[$key]) {
  55. $value = false;
  56. } elseif (false === $value = unserialize($value)) {
  57. $this->values[$key] = $value = null;
  58. $isHit = false;
  59. }
  60. } catch (\Exception $e) {
  61. CacheItem::log($this->logger, 'Failed to unserialize key "{key}"', ['key' => $key, 'exception' => $e]);
  62. $this->values[$key] = $value = null;
  63. $isHit = false;
  64. }
  65. $f = $this->createCacheItem;
  66. return $f($key, $value, $isHit);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getItems(array $keys = [])
  72. {
  73. foreach ($keys as $key) {
  74. CacheItem::validateKey($key);
  75. }
  76. return $this->generateItems($keys, time(), $this->createCacheItem);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function deleteItems(array $keys)
  82. {
  83. foreach ($keys as $key) {
  84. $this->deleteItem($key);
  85. }
  86. return true;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function save(CacheItemInterface $item)
  92. {
  93. if (!$item instanceof CacheItem) {
  94. return false;
  95. }
  96. $item = (array) $item;
  97. $key = $item["\0*\0key"];
  98. $value = $item["\0*\0value"];
  99. $expiry = $item["\0*\0expiry"];
  100. if (null !== $expiry && $expiry <= time()) {
  101. $this->deleteItem($key);
  102. return true;
  103. }
  104. if ($this->storeSerialized) {
  105. try {
  106. $value = serialize($value);
  107. } catch (\Exception $e) {
  108. $type = \is_object($value) ? \get_class($value) : \gettype($value);
  109. CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', ['key' => $key, 'type' => $type, 'exception' => $e]);
  110. return false;
  111. }
  112. }
  113. if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
  114. $expiry = time() + $item["\0*\0defaultLifetime"];
  115. }
  116. $this->values[$key] = $value;
  117. $this->expiries[$key] = null !== $expiry ? $expiry : PHP_INT_MAX;
  118. return true;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function saveDeferred(CacheItemInterface $item)
  124. {
  125. return $this->save($item);
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function commit()
  131. {
  132. return true;
  133. }
  134. }