CacheItem.php 5.2 KB

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