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.

CacheItem.php 5.7KB

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