You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArrayAdapter.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /**
  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. static 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 get(string $key, callable $callback, float $beta = null, array &$metadata = null)
  47. {
  48. $item = $this->getItem($key);
  49. $metadata = $item->getMetadata();
  50. // ArrayAdapter works in memory, we don't care about stampede protection
  51. if (INF === $beta || !$item->isHit()) {
  52. $save = true;
  53. $this->save($item->set($callback($item, $save)));
  54. }
  55. return $item->get();
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getItem($key)
  61. {
  62. if (!$isHit = $this->hasItem($key)) {
  63. $this->values[$key] = $value = null;
  64. } else {
  65. $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
  66. }
  67. $f = $this->createCacheItem;
  68. return $f($key, $value, $isHit);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getItems(array $keys = [])
  74. {
  75. foreach ($keys as $key) {
  76. if (!\is_string($key) || !isset($this->expiries[$key])) {
  77. CacheItem::validateKey($key);
  78. }
  79. }
  80. return $this->generateItems($keys, microtime(true), $this->createCacheItem);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function deleteItems(array $keys)
  86. {
  87. foreach ($keys as $key) {
  88. $this->deleteItem($key);
  89. }
  90. return true;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function save(CacheItemInterface $item)
  96. {
  97. if (!$item instanceof CacheItem) {
  98. return false;
  99. }
  100. $item = (array) $item;
  101. $key = $item["\0*\0key"];
  102. $value = $item["\0*\0value"];
  103. $expiry = $item["\0*\0expiry"];
  104. if (null !== $expiry && $expiry <= microtime(true)) {
  105. $this->deleteItem($key);
  106. return true;
  107. }
  108. if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) {
  109. return false;
  110. }
  111. if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
  112. $expiry = microtime(true) + $item["\0*\0defaultLifetime"];
  113. }
  114. $this->values[$key] = $value;
  115. $this->expiries[$key] = null !== $expiry ? $expiry : PHP_INT_MAX;
  116. return true;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function saveDeferred(CacheItemInterface $item)
  122. {
  123. return $this->save($item);
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function commit()
  129. {
  130. return true;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function delete(string $key): bool
  136. {
  137. return $this->deleteItem($key);
  138. }
  139. }