ArrayCache.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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;
  13. use Symfony\Component\Cache\CacheItem;
  14. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  15. use Symfony\Component\Cache\ResettableInterface;
  16. use Symfony\Component\Cache\Traits\ArrayTrait;
  17. /**
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. class ArrayCache implements CacheInterface, LoggerAwareInterface, ResettableInterface
  21. {
  22. use ArrayTrait {
  23. ArrayTrait::deleteItem as delete;
  24. ArrayTrait::hasItem as has;
  25. }
  26. private $defaultLifetime;
  27. /**
  28. * @param int $defaultLifetime
  29. * @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
  30. */
  31. public function __construct(int $defaultLifetime = 0, bool $storeSerialized = true)
  32. {
  33. $this->defaultLifetime = $defaultLifetime;
  34. $this->storeSerialized = $storeSerialized;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function get($key, $default = null)
  40. {
  41. foreach ($this->getMultiple([$key], $default) as $v) {
  42. return $v;
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getMultiple($keys, $default = null)
  49. {
  50. if ($keys instanceof \Traversable) {
  51. $keys = iterator_to_array($keys, false);
  52. } elseif (!\is_array($keys)) {
  53. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  54. }
  55. foreach ($keys as $key) {
  56. CacheItem::validateKey($key);
  57. }
  58. return $this->generateItems($keys, time(), function ($k, $v, $hit) use ($default) { return $hit ? $v : $default; });
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function deleteMultiple($keys)
  64. {
  65. if (!\is_array($keys) && !$keys instanceof \Traversable) {
  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. $this->delete($key);
  70. }
  71. return true;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function set($key, $value, $ttl = null)
  77. {
  78. CacheItem::validateKey($key);
  79. return $this->setMultiple([$key => $value], $ttl);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function setMultiple($values, $ttl = null)
  85. {
  86. if (!\is_array($values) && !$values instanceof \Traversable) {
  87. throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', \is_object($values) ? \get_class($values) : \gettype($values)));
  88. }
  89. $valuesArray = [];
  90. foreach ($values as $key => $value) {
  91. \is_int($key) || CacheItem::validateKey($key);
  92. $valuesArray[$key] = $value;
  93. }
  94. if (false === $ttl = $this->normalizeTtl($ttl)) {
  95. return $this->deleteMultiple(array_keys($valuesArray));
  96. }
  97. if ($this->storeSerialized) {
  98. foreach ($valuesArray as $key => $value) {
  99. try {
  100. $valuesArray[$key] = serialize($value);
  101. } catch (\Exception $e) {
  102. $type = \is_object($value) ? \get_class($value) : \gettype($value);
  103. CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', ['key' => $key, 'type' => $type, 'exception' => $e]);
  104. return false;
  105. }
  106. }
  107. }
  108. $expiry = 0 < $ttl ? time() + $ttl : PHP_INT_MAX;
  109. foreach ($valuesArray as $key => $value) {
  110. $this->values[$key] = $value;
  111. $this->expiries[$key] = $expiry;
  112. }
  113. return true;
  114. }
  115. private function normalizeTtl($ttl)
  116. {
  117. if (null === $ttl) {
  118. return $this->defaultLifetime;
  119. }
  120. if ($ttl instanceof \DateInterval) {
  121. $ttl = (int) \DateTime::createFromFormat('U', 0)->add($ttl)->format('U');
  122. }
  123. if (\is_int($ttl)) {
  124. return 0 < $ttl ? $ttl : false;
  125. }
  126. throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given', \is_object($ttl) ? \get_class($ttl) : \gettype($ttl)));
  127. }
  128. }