ChainCache.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\SimpleCache\CacheInterface;
  12. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  13. use Symfony\Component\Cache\PruneableInterface;
  14. use Symfony\Component\Cache\ResettableInterface;
  15. /**
  16. * Chains several caches together.
  17. *
  18. * Cached items are fetched from the first cache having them in its data store.
  19. * They are saved and deleted in all caches at once.
  20. *
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. */
  23. class ChainCache implements CacheInterface, PruneableInterface, ResettableInterface
  24. {
  25. private $miss;
  26. private $caches = [];
  27. private $defaultLifetime;
  28. private $cacheCount;
  29. /**
  30. * @param CacheInterface[] $caches The ordered list of caches used to fetch cached items
  31. * @param int $defaultLifetime The lifetime of items propagated from lower caches to upper ones
  32. */
  33. public function __construct(array $caches, int $defaultLifetime = 0)
  34. {
  35. if (!$caches) {
  36. throw new InvalidArgumentException('At least one cache must be specified.');
  37. }
  38. foreach ($caches as $cache) {
  39. if (!$cache instanceof CacheInterface) {
  40. throw new InvalidArgumentException(sprintf('The class "%s" does not implement the "%s" interface.', \get_class($cache), CacheInterface::class));
  41. }
  42. }
  43. $this->miss = new \stdClass();
  44. $this->caches = array_values($caches);
  45. $this->cacheCount = \count($this->caches);
  46. $this->defaultLifetime = 0 < $defaultLifetime ? $defaultLifetime : null;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function get($key, $default = null)
  52. {
  53. $miss = null !== $default && \is_object($default) ? $default : $this->miss;
  54. foreach ($this->caches as $i => $cache) {
  55. $value = $cache->get($key, $miss);
  56. if ($miss !== $value) {
  57. while (0 <= --$i) {
  58. $this->caches[$i]->set($key, $value, $this->defaultLifetime);
  59. }
  60. return $value;
  61. }
  62. }
  63. return $default;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getMultiple($keys, $default = null)
  69. {
  70. $miss = null !== $default && \is_object($default) ? $default : $this->miss;
  71. return $this->generateItems($this->caches[0]->getMultiple($keys, $miss), 0, $miss, $default);
  72. }
  73. private function generateItems($values, $cacheIndex, $miss, $default)
  74. {
  75. $missing = [];
  76. $nextCacheIndex = $cacheIndex + 1;
  77. $nextCache = isset($this->caches[$nextCacheIndex]) ? $this->caches[$nextCacheIndex] : null;
  78. foreach ($values as $k => $value) {
  79. if ($miss !== $value) {
  80. yield $k => $value;
  81. } elseif (!$nextCache) {
  82. yield $k => $default;
  83. } else {
  84. $missing[] = $k;
  85. }
  86. }
  87. if ($missing) {
  88. $cache = $this->caches[$cacheIndex];
  89. $values = $this->generateItems($nextCache->getMultiple($missing, $miss), $nextCacheIndex, $miss, $default);
  90. foreach ($values as $k => $value) {
  91. if ($miss !== $value) {
  92. $cache->set($k, $value, $this->defaultLifetime);
  93. yield $k => $value;
  94. } else {
  95. yield $k => $default;
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function has($key)
  104. {
  105. foreach ($this->caches as $cache) {
  106. if ($cache->has($key)) {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function clear()
  116. {
  117. $cleared = true;
  118. $i = $this->cacheCount;
  119. while ($i--) {
  120. $cleared = $this->caches[$i]->clear() && $cleared;
  121. }
  122. return $cleared;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function delete($key)
  128. {
  129. $deleted = true;
  130. $i = $this->cacheCount;
  131. while ($i--) {
  132. $deleted = $this->caches[$i]->delete($key) && $deleted;
  133. }
  134. return $deleted;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function deleteMultiple($keys)
  140. {
  141. if ($keys instanceof \Traversable) {
  142. $keys = iterator_to_array($keys, false);
  143. }
  144. $deleted = true;
  145. $i = $this->cacheCount;
  146. while ($i--) {
  147. $deleted = $this->caches[$i]->deleteMultiple($keys) && $deleted;
  148. }
  149. return $deleted;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function set($key, $value, $ttl = null)
  155. {
  156. $saved = true;
  157. $i = $this->cacheCount;
  158. while ($i--) {
  159. $saved = $this->caches[$i]->set($key, $value, $ttl) && $saved;
  160. }
  161. return $saved;
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function setMultiple($values, $ttl = null)
  167. {
  168. if ($values instanceof \Traversable) {
  169. $valuesIterator = $values;
  170. $values = function () use ($valuesIterator, &$values) {
  171. $generatedValues = [];
  172. foreach ($valuesIterator as $key => $value) {
  173. yield $key => $value;
  174. $generatedValues[$key] = $value;
  175. }
  176. $values = $generatedValues;
  177. };
  178. $values = $values();
  179. }
  180. $saved = true;
  181. $i = $this->cacheCount;
  182. while ($i--) {
  183. $saved = $this->caches[$i]->setMultiple($values, $ttl) && $saved;
  184. }
  185. return $saved;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function prune()
  191. {
  192. $pruned = true;
  193. foreach ($this->caches as $cache) {
  194. if ($cache instanceof PruneableInterface) {
  195. $pruned = $cache->prune() && $pruned;
  196. }
  197. }
  198. return $pruned;
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function reset()
  204. {
  205. foreach ($this->caches as $cache) {
  206. if ($cache instanceof ResettableInterface) {
  207. $cache->reset();
  208. }
  209. }
  210. }
  211. }