AbstractCache.php 5.7 KB

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