AbstractCache.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\AbstractAdapter;
  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\AbstractTrait;
  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.', AbstractCache::class, AbstractAdapter::class, CacheInterface::class), \E_USER_DEPRECATED);
  20. /**
  21. * @deprecated since Symfony 4.3, use AbstractAdapter and type-hint for CacheInterface instead.
  22. */
  23. abstract class AbstractCache implements Psr16CacheInterface, LoggerAwareInterface, ResettableInterface
  24. {
  25. use AbstractTrait {
  26. deleteItems as private;
  27. AbstractTrait::deleteItem as delete;
  28. AbstractTrait::hasItem as has;
  29. }
  30. /**
  31. * @internal
  32. */
  33. protected const NS_SEPARATOR = ':';
  34. private $defaultLifetime;
  35. protected function __construct(string $namespace = '', int $defaultLifetime = 0)
  36. {
  37. $this->defaultLifetime = max(0, $defaultLifetime);
  38. $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).':';
  39. if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) {
  40. throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s").', $this->maxIdLength - 24, \strlen($namespace), $namespace));
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function get($key, $default = null)
  47. {
  48. $id = $this->getId($key);
  49. try {
  50. foreach ($this->doFetch([$id]) as $value) {
  51. return $value;
  52. }
  53. } catch (\Exception $e) {
  54. CacheItem::log($this->logger, 'Failed to fetch key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e]);
  55. }
  56. return $default;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. *
  61. * @return bool
  62. */
  63. public function set($key, $value, $ttl = null)
  64. {
  65. CacheItem::validateKey($key);
  66. return $this->setMultiple([$key => $value], $ttl);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. *
  71. * @return iterable
  72. */
  73. public function getMultiple($keys, $default = null)
  74. {
  75. if ($keys instanceof \Traversable) {
  76. $keys = iterator_to_array($keys, false);
  77. } elseif (!\is_array($keys)) {
  78. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  79. }
  80. $ids = [];
  81. foreach ($keys as $key) {
  82. $ids[] = $this->getId($key);
  83. }
  84. try {
  85. $values = $this->doFetch($ids);
  86. } catch (\Exception $e) {
  87. CacheItem::log($this->logger, 'Failed to fetch values: '.$e->getMessage(), ['keys' => $keys, 'exception' => $e]);
  88. $values = [];
  89. }
  90. $ids = array_combine($ids, $keys);
  91. return $this->generateValues($values, $ids, $default);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. *
  96. * @return bool
  97. */
  98. public function setMultiple($values, $ttl = null)
  99. {
  100. if (!\is_array($values) && !$values instanceof \Traversable) {
  101. throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given.', \is_object($values) ? \get_class($values) : \gettype($values)));
  102. }
  103. $valuesById = [];
  104. foreach ($values as $key => $value) {
  105. if (\is_int($key)) {
  106. $key = (string) $key;
  107. }
  108. $valuesById[$this->getId($key)] = $value;
  109. }
  110. if (false === $ttl = $this->normalizeTtl($ttl)) {
  111. return $this->doDelete(array_keys($valuesById));
  112. }
  113. try {
  114. $e = $this->doSave($valuesById, $ttl);
  115. } catch (\Exception $e) {
  116. }
  117. if (true === $e || [] === $e) {
  118. return true;
  119. }
  120. $keys = [];
  121. foreach (\is_array($e) ? $e : array_keys($valuesById) as $id) {
  122. $keys[] = substr($id, \strlen($this->namespace));
  123. }
  124. $message = 'Failed to save values'.($e instanceof \Exception ? ': '.$e->getMessage() : '.');
  125. CacheItem::log($this->logger, $message, ['keys' => $keys, 'exception' => $e instanceof \Exception ? $e : null]);
  126. return false;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. *
  131. * @return bool
  132. */
  133. public function deleteMultiple($keys)
  134. {
  135. if ($keys instanceof \Traversable) {
  136. $keys = iterator_to_array($keys, false);
  137. } elseif (!\is_array($keys)) {
  138. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  139. }
  140. return $this->deleteItems($keys);
  141. }
  142. private function normalizeTtl($ttl)
  143. {
  144. if (null === $ttl) {
  145. return $this->defaultLifetime;
  146. }
  147. if ($ttl instanceof \DateInterval) {
  148. $ttl = (int) \DateTime::createFromFormat('U', 0)->add($ttl)->format('U');
  149. }
  150. if (\is_int($ttl)) {
  151. return 0 < $ttl ? $ttl : false;
  152. }
  153. throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given.', \is_object($ttl) ? \get_class($ttl) : \gettype($ttl)));
  154. }
  155. private function generateValues(iterable $values, array &$keys, $default): iterable
  156. {
  157. try {
  158. foreach ($values as $id => $value) {
  159. if (!isset($keys[$id])) {
  160. throw new InvalidArgumentException(sprintf('Could not match value id "%s" to keys "%s".', $id, implode('", "', $keys)));
  161. }
  162. $key = $keys[$id];
  163. unset($keys[$id]);
  164. yield $key => $value;
  165. }
  166. } catch (\Exception $e) {
  167. CacheItem::log($this->logger, 'Failed to fetch values: '.$e->getMessage(), ['keys' => array_values($keys), 'exception' => $e]);
  168. }
  169. foreach ($keys as $key) {
  170. yield $key => $default;
  171. }
  172. }
  173. }