ArrayCache.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Simple;
  11. use Psr\Log\LoggerAwareInterface;
  12. use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
  13. use Symfony\Component\Cache\Adapter\ArrayAdapter;
  14. use Symfony\Component\Cache\CacheItem;
  15. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  16. use Symfony\Component\Cache\ResettableInterface;
  17. use Symfony\Component\Cache\Traits\ArrayTrait;
  18. use Symfony\Contracts\Cache\CacheInterface;
  19. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', ArrayCache::class, ArrayAdapter::class, CacheInterface::class), \E_USER_DEPRECATED);
  20. /**
  21. * @deprecated since Symfony 4.3, use ArrayAdapter and type-hint for CacheInterface instead.
  22. */
  23. class ArrayCache implements Psr16CacheInterface, LoggerAwareInterface, ResettableInterface
  24. {
  25. use ArrayTrait {
  26. ArrayTrait::deleteItem as delete;
  27. ArrayTrait::hasItem as has;
  28. }
  29. private $defaultLifetime;
  30. /**
  31. * @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
  32. */
  33. public function __construct(int $defaultLifetime = 0, bool $storeSerialized = true)
  34. {
  35. $this->defaultLifetime = $defaultLifetime;
  36. $this->storeSerialized = $storeSerialized;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function get($key, $default = null)
  42. {
  43. if (!\is_string($key) || !isset($this->expiries[$key])) {
  44. CacheItem::validateKey($key);
  45. }
  46. if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] > microtime(true) || !$this->delete($key))) {
  47. $this->values[$key] = null;
  48. return $default;
  49. }
  50. if (!$this->storeSerialized) {
  51. return $this->values[$key];
  52. }
  53. $value = $this->unfreeze($key, $isHit);
  54. return $isHit ? $value : $default;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. *
  59. * @return iterable
  60. */
  61. public function getMultiple($keys, $default = null)
  62. {
  63. if ($keys instanceof \Traversable) {
  64. $keys = iterator_to_array($keys, false);
  65. } elseif (!\is_array($keys)) {
  66. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  67. }
  68. foreach ($keys as $key) {
  69. if (!\is_string($key) || !isset($this->expiries[$key])) {
  70. CacheItem::validateKey($key);
  71. }
  72. }
  73. return $this->generateItems($keys, microtime(true), function ($k, $v, $hit) use ($default) { return $hit ? $v : $default; });
  74. }
  75. /**
  76. * {@inheritdoc}
  77. *
  78. * @return bool
  79. */
  80. public function deleteMultiple($keys)
  81. {
  82. if (!\is_array($keys) && !$keys instanceof \Traversable) {
  83. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  84. }
  85. foreach ($keys as $key) {
  86. $this->delete($key);
  87. }
  88. return true;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. *
  93. * @return bool
  94. */
  95. public function set($key, $value, $ttl = null)
  96. {
  97. if (!\is_string($key)) {
  98. CacheItem::validateKey($key);
  99. }
  100. return $this->setMultiple([$key => $value], $ttl);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. *
  105. * @return bool
  106. */
  107. public function setMultiple($values, $ttl = null)
  108. {
  109. if (!\is_array($values) && !$values instanceof \Traversable) {
  110. throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given.', \is_object($values) ? \get_class($values) : \gettype($values)));
  111. }
  112. $valuesArray = [];
  113. foreach ($values as $key => $value) {
  114. if (!\is_int($key) && !(\is_string($key) && isset($this->expiries[$key]))) {
  115. CacheItem::validateKey($key);
  116. }
  117. $valuesArray[$key] = $value;
  118. }
  119. if (false === $ttl = $this->normalizeTtl($ttl)) {
  120. return $this->deleteMultiple(array_keys($valuesArray));
  121. }
  122. $expiry = 0 < $ttl ? microtime(true) + $ttl : \PHP_INT_MAX;
  123. foreach ($valuesArray as $key => $value) {
  124. if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) {
  125. return false;
  126. }
  127. $this->values[$key] = $value;
  128. $this->expiries[$key] = $expiry;
  129. }
  130. return true;
  131. }
  132. private function normalizeTtl($ttl)
  133. {
  134. if (null === $ttl) {
  135. return $this->defaultLifetime;
  136. }
  137. if ($ttl instanceof \DateInterval) {
  138. $ttl = (int) \DateTime::createFromFormat('U', 0)->add($ttl)->format('U');
  139. }
  140. if (\is_int($ttl)) {
  141. return 0 < $ttl ? $ttl : false;
  142. }
  143. throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given.', \is_object($ttl) ? \get_class($ttl) : \gettype($ttl)));
  144. }
  145. }