ArrayAdapter.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. use Symfony\Contracts\Cache\CacheInterface;
  17. /**
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
  21. {
  22. use ArrayTrait;
  23. private $createCacheItem;
  24. private $defaultLifetime;
  25. /**
  26. * @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
  27. */
  28. public function __construct(int $defaultLifetime = 0, bool $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 get(string $key, callable $callback, float $beta = null, array &$metadata = null)
  48. {
  49. $item = $this->getItem($key);
  50. $metadata = $item->getMetadata();
  51. // ArrayAdapter works in memory, we don't care about stampede protection
  52. if (\INF === $beta || !$item->isHit()) {
  53. $save = true;
  54. $item->set($callback($item, $save));
  55. if ($save) {
  56. $this->save($item);
  57. }
  58. }
  59. return $item->get();
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getItem($key)
  65. {
  66. if (!$isHit = $this->hasItem($key)) {
  67. $this->values[$key] = $value = null;
  68. } else {
  69. $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
  70. }
  71. $f = $this->createCacheItem;
  72. return $f($key, $value, $isHit);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getItems(array $keys = [])
  78. {
  79. foreach ($keys as $key) {
  80. if (!\is_string($key) || !isset($this->expiries[$key])) {
  81. CacheItem::validateKey($key);
  82. }
  83. }
  84. return $this->generateItems($keys, microtime(true), $this->createCacheItem);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. *
  89. * @return bool
  90. */
  91. public function deleteItems(array $keys)
  92. {
  93. foreach ($keys as $key) {
  94. $this->deleteItem($key);
  95. }
  96. return true;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. *
  101. * @return bool
  102. */
  103. public function save(CacheItemInterface $item)
  104. {
  105. if (!$item instanceof CacheItem) {
  106. return false;
  107. }
  108. $item = (array) $item;
  109. $key = $item["\0*\0key"];
  110. $value = $item["\0*\0value"];
  111. $expiry = $item["\0*\0expiry"];
  112. if (null !== $expiry) {
  113. if (!$expiry) {
  114. $expiry = \PHP_INT_MAX;
  115. } elseif ($expiry <= microtime(true)) {
  116. $this->deleteItem($key);
  117. return true;
  118. }
  119. }
  120. if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) {
  121. return false;
  122. }
  123. if (null === $expiry && 0 < $this->defaultLifetime) {
  124. $expiry = microtime(true) + $this->defaultLifetime;
  125. }
  126. $this->values[$key] = $value;
  127. $this->expiries[$key] = $expiry ?? \PHP_INT_MAX;
  128. return true;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. *
  133. * @return bool
  134. */
  135. public function saveDeferred(CacheItemInterface $item)
  136. {
  137. return $this->save($item);
  138. }
  139. /**
  140. * {@inheritdoc}
  141. *
  142. * @return bool
  143. */
  144. public function commit()
  145. {
  146. return true;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function delete(string $key): bool
  152. {
  153. return $this->deleteItem($key);
  154. }
  155. }