CacheItem.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  13. use Symfony\Component\Cache\Exception\LogicException;
  14. use Symfony\Contracts\Cache\ItemInterface;
  15. /**
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. final class CacheItem implements ItemInterface
  19. {
  20. private const METADATA_EXPIRY_OFFSET = 1527506807;
  21. protected $key;
  22. protected $value;
  23. protected $isHit = false;
  24. protected $expiry;
  25. protected $metadata = [];
  26. protected $newMetadata = [];
  27. protected $innerItem;
  28. protected $poolHash;
  29. protected $isTaggable = false;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getKey(): string
  34. {
  35. return $this->key;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function get()
  41. {
  42. return $this->value;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function isHit(): bool
  48. {
  49. return $this->isHit;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. *
  54. * @return $this
  55. */
  56. public function set($value): self
  57. {
  58. $this->value = $value;
  59. return $this;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. *
  64. * @return $this
  65. */
  66. public function expiresAt($expiration): self
  67. {
  68. if (null === $expiration) {
  69. $this->expiry = null;
  70. } elseif ($expiration instanceof \DateTimeInterface) {
  71. $this->expiry = (float) $expiration->format('U.u');
  72. } else {
  73. throw new InvalidArgumentException(sprintf('Expiration date must implement DateTimeInterface or be null, "%s" given.', get_debug_type($expiration)));
  74. }
  75. return $this;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. *
  80. * @return $this
  81. */
  82. public function expiresAfter($time): self
  83. {
  84. if (null === $time) {
  85. $this->expiry = null;
  86. } elseif ($time instanceof \DateInterval) {
  87. $this->expiry = microtime(true) + \DateTime::createFromFormat('U', 0)->add($time)->format('U.u');
  88. } elseif (\is_int($time)) {
  89. $this->expiry = $time + microtime(true);
  90. } else {
  91. throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given.', get_debug_type($time)));
  92. }
  93. return $this;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function tag($tags): ItemInterface
  99. {
  100. if (!$this->isTaggable) {
  101. throw new LogicException(sprintf('Cache item "%s" comes from a non tag-aware pool: you cannot tag it.', $this->key));
  102. }
  103. if (!is_iterable($tags)) {
  104. $tags = [$tags];
  105. }
  106. foreach ($tags as $tag) {
  107. if (!\is_string($tag)) {
  108. throw new InvalidArgumentException(sprintf('Cache tag must be string, "%s" given.', get_debug_type($tag)));
  109. }
  110. if (isset($this->newMetadata[self::METADATA_TAGS][$tag])) {
  111. continue;
  112. }
  113. if ('' === $tag) {
  114. throw new InvalidArgumentException('Cache tag length must be greater than zero.');
  115. }
  116. if (false !== strpbrk($tag, self::RESERVED_CHARACTERS)) {
  117. throw new InvalidArgumentException(sprintf('Cache tag "%s" contains reserved characters "%s".', $tag, self::RESERVED_CHARACTERS));
  118. }
  119. $this->newMetadata[self::METADATA_TAGS][$tag] = $tag;
  120. }
  121. return $this;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getMetadata(): array
  127. {
  128. return $this->metadata;
  129. }
  130. /**
  131. * Validates a cache key according to PSR-6.
  132. *
  133. * @param string $key The key to validate
  134. *
  135. * @throws InvalidArgumentException When $key is not valid
  136. */
  137. public static function validateKey($key): string
  138. {
  139. if (!\is_string($key)) {
  140. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', get_debug_type($key)));
  141. }
  142. if ('' === $key) {
  143. throw new InvalidArgumentException('Cache key length must be greater than zero.');
  144. }
  145. if (false !== strpbrk($key, self::RESERVED_CHARACTERS)) {
  146. throw new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters "%s".', $key, self::RESERVED_CHARACTERS));
  147. }
  148. return $key;
  149. }
  150. /**
  151. * Internal logging helper.
  152. *
  153. * @internal
  154. */
  155. public static function log(?LoggerInterface $logger, string $message, array $context = [])
  156. {
  157. if ($logger) {
  158. $logger->warning($message, $context);
  159. } else {
  160. $replace = [];
  161. foreach ($context as $k => $v) {
  162. if (is_scalar($v)) {
  163. $replace['{'.$k.'}'] = $v;
  164. }
  165. }
  166. @trigger_error(strtr($message, $replace), E_USER_WARNING);
  167. }
  168. }
  169. }