ArrayAdapter.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. $this->save($item->set($callback($item, $save)));
  55. }
  56. return $item->get();
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getItem($key)
  62. {
  63. if (!$isHit = $this->hasItem($key)) {
  64. $this->values[$key] = $value = null;
  65. } else {
  66. $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
  67. }
  68. $f = $this->createCacheItem;
  69. return $f($key, $value, $isHit);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getItems(array $keys = [])
  75. {
  76. foreach ($keys as $key) {
  77. if (!\is_string($key) || !isset($this->expiries[$key])) {
  78. CacheItem::validateKey($key);
  79. }
  80. }
  81. return $this->generateItems($keys, microtime(true), $this->createCacheItem);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. *
  86. * @return bool
  87. */
  88. public function deleteItems(array $keys)
  89. {
  90. foreach ($keys as $key) {
  91. $this->deleteItem($key);
  92. }
  93. return true;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. *
  98. * @return bool
  99. */
  100. public function save(CacheItemInterface $item)
  101. {
  102. if (!$item instanceof CacheItem) {
  103. return false;
  104. }
  105. $item = (array) $item;
  106. $key = $item["\0*\0key"];
  107. $value = $item["\0*\0value"];
  108. $expiry = $item["\0*\0expiry"];
  109. if (null !== $expiry) {
  110. if (!$expiry) {
  111. $expiry = \PHP_INT_MAX;
  112. } elseif ($expiry <= microtime(true)) {
  113. $this->deleteItem($key);
  114. return true;
  115. }
  116. }
  117. if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) {
  118. return false;
  119. }
  120. if (null === $expiry && 0 < $this->defaultLifetime) {
  121. $expiry = microtime(true) + $this->defaultLifetime;
  122. }
  123. $this->values[$key] = $value;
  124. $this->expiries[$key] = $expiry ?? \PHP_INT_MAX;
  125. return true;
  126. }
  127. /**
  128. * {@inheritdoc}
  129. *
  130. * @return bool
  131. */
  132. public function saveDeferred(CacheItemInterface $item)
  133. {
  134. return $this->save($item);
  135. }
  136. /**
  137. * {@inheritdoc}
  138. *
  139. * @return bool
  140. */
  141. public function commit()
  142. {
  143. return true;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function delete(string $key): bool
  149. {
  150. return $this->deleteItem($key);
  151. }
  152. }