ChainCache.php 6.9 KB

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