ArrayAdapter.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. private $defaultLifetime;
  24. /**
  25. * @param int $defaultLifetime
  26. * @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
  27. */
  28. public function __construct($defaultLifetime = 0, $storeSerialized = true)
  29. {
  30. $this->defaultLifetime = $defaultLifetime;
  31. $this->storeSerialized = $storeSerialized;
  32. $this->createCacheItem = \Closure::bind(
  33. static function ($key, $value, $isHit) {
  34. $item = new CacheItem();
  35. $item->key = $key;
  36. $item->value = $value;
  37. $item->isHit = $isHit;
  38. return $item;
  39. },
  40. null,
  41. CacheItem::class
  42. );
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getItem($key)
  48. {
  49. $isHit = $this->hasItem($key);
  50. try {
  51. if (!$isHit) {
  52. $this->values[$key] = $value = null;
  53. } elseif (!$this->storeSerialized) {
  54. $value = $this->values[$key];
  55. } elseif ('b:0;' === $value = $this->values[$key]) {
  56. $value = false;
  57. } elseif (false === $value = unserialize($value)) {
  58. $this->values[$key] = $value = null;
  59. $isHit = false;
  60. }
  61. } catch (\Exception $e) {
  62. CacheItem::log($this->logger, 'Failed to unserialize key "{key}"', ['key' => $key, 'exception' => $e]);
  63. $this->values[$key] = $value = null;
  64. $isHit = false;
  65. }
  66. $f = $this->createCacheItem;
  67. return $f($key, $value, $isHit);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getItems(array $keys = [])
  73. {
  74. foreach ($keys as $key) {
  75. CacheItem::validateKey($key);
  76. }
  77. return $this->generateItems($keys, time(), $this->createCacheItem);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function deleteItems(array $keys)
  83. {
  84. foreach ($keys as $key) {
  85. $this->deleteItem($key);
  86. }
  87. return true;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function save(CacheItemInterface $item)
  93. {
  94. if (!$item instanceof CacheItem) {
  95. return false;
  96. }
  97. $item = (array) $item;
  98. $key = $item["\0*\0key"];
  99. $value = $item["\0*\0value"];
  100. $expiry = $item["\0*\0expiry"];
  101. if (0 === $expiry) {
  102. $expiry = \PHP_INT_MAX;
  103. }
  104. if (null !== $expiry && $expiry <= time()) {
  105. $this->deleteItem($key);
  106. return true;
  107. }
  108. if ($this->storeSerialized) {
  109. try {
  110. $value = serialize($value);
  111. } catch (\Exception $e) {
  112. $type = \is_object($value) ? \get_class($value) : \gettype($value);
  113. CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', ['key' => $key, 'type' => $type, 'exception' => $e]);
  114. return false;
  115. }
  116. }
  117. if (null === $expiry && 0 < $this->defaultLifetime) {
  118. $expiry = time() + $this->defaultLifetime;
  119. }
  120. $this->values[$key] = $value;
  121. $this->expiries[$key] = null !== $expiry ? $expiry : \PHP_INT_MAX;
  122. return true;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function saveDeferred(CacheItemInterface $item)
  128. {
  129. return $this->save($item);
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function commit()
  135. {
  136. return true;
  137. }
  138. }