ChainAdapter.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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\Adapter;
  11. use Psr\Cache\CacheItemInterface;
  12. use Psr\Cache\CacheItemPoolInterface;
  13. use Symfony\Component\Cache\CacheItem;
  14. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  15. use Symfony\Component\Cache\PruneableInterface;
  16. use Symfony\Component\Cache\ResettableInterface;
  17. use Symfony\Component\Cache\Traits\ContractsTrait;
  18. use Symfony\Contracts\Cache\CacheInterface;
  19. use Symfony\Contracts\Service\ResetInterface;
  20. /**
  21. * Chains several adapters together.
  22. *
  23. * Cached items are fetched from the first adapter having them in its data store.
  24. * They are saved and deleted in all adapters at once.
  25. *
  26. * @author Kévin Dunglas <dunglas@gmail.com>
  27. */
  28. class ChainAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
  29. {
  30. use ContractsTrait;
  31. private $adapters = [];
  32. private $adapterCount;
  33. private $syncItem;
  34. /**
  35. * @param CacheItemPoolInterface[] $adapters The ordered list of adapters used to fetch cached items
  36. * @param int $defaultLifetime The default lifetime of items propagated from lower adapters to upper ones
  37. */
  38. public function __construct(array $adapters, int $defaultLifetime = 0)
  39. {
  40. if (!$adapters) {
  41. throw new InvalidArgumentException('At least one adapter must be specified.');
  42. }
  43. foreach ($adapters as $adapter) {
  44. if (!$adapter instanceof CacheItemPoolInterface) {
  45. throw new InvalidArgumentException(sprintf('The class "%s" does not implement the "%s" interface.', \get_class($adapter), CacheItemPoolInterface::class));
  46. }
  47. if ($adapter instanceof AdapterInterface) {
  48. $this->adapters[] = $adapter;
  49. } else {
  50. $this->adapters[] = new ProxyAdapter($adapter);
  51. }
  52. }
  53. $this->adapterCount = \count($this->adapters);
  54. $this->syncItem = \Closure::bind(
  55. static function ($sourceItem, $item) use ($defaultLifetime) {
  56. $item->value = $sourceItem->value;
  57. $item->expiry = $sourceItem->expiry;
  58. $item->isHit = $sourceItem->isHit;
  59. $item->metadata = $sourceItem->metadata;
  60. $sourceItem->isTaggable = false;
  61. unset($sourceItem->metadata[CacheItem::METADATA_TAGS]);
  62. if (0 < $sourceItem->defaultLifetime && $sourceItem->defaultLifetime < $defaultLifetime) {
  63. $defaultLifetime = $sourceItem->defaultLifetime;
  64. }
  65. if (0 < $defaultLifetime && ($item->defaultLifetime <= 0 || $defaultLifetime < $item->defaultLifetime)) {
  66. $item->defaultLifetime = $defaultLifetime;
  67. }
  68. return $item;
  69. },
  70. null,
  71. CacheItem::class
  72. );
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
  78. {
  79. $lastItem = null;
  80. $i = 0;
  81. $wrap = function (CacheItem $item = null) use ($key, $callback, $beta, &$wrap, &$i, &$lastItem, &$metadata) {
  82. $adapter = $this->adapters[$i];
  83. if (isset($this->adapters[++$i])) {
  84. $callback = $wrap;
  85. $beta = INF === $beta ? INF : 0;
  86. }
  87. if ($adapter instanceof CacheInterface) {
  88. $value = $adapter->get($key, $callback, $beta, $metadata);
  89. } else {
  90. $value = $this->doGet($adapter, $key, $callback, $beta, $metadata);
  91. }
  92. if (null !== $item) {
  93. ($this->syncItem)($lastItem = $lastItem ?? $item, $item);
  94. }
  95. return $value;
  96. };
  97. return $wrap();
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function getItem($key)
  103. {
  104. $syncItem = $this->syncItem;
  105. $misses = [];
  106. foreach ($this->adapters as $i => $adapter) {
  107. $item = $adapter->getItem($key);
  108. if ($item->isHit()) {
  109. while (0 <= --$i) {
  110. $this->adapters[$i]->save($syncItem($item, $misses[$i]));
  111. }
  112. return $item;
  113. }
  114. $misses[$i] = $item;
  115. }
  116. return $item;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function getItems(array $keys = [])
  122. {
  123. return $this->generateItems($this->adapters[0]->getItems($keys), 0);
  124. }
  125. private function generateItems($items, $adapterIndex)
  126. {
  127. $missing = [];
  128. $misses = [];
  129. $nextAdapterIndex = $adapterIndex + 1;
  130. $nextAdapter = isset($this->adapters[$nextAdapterIndex]) ? $this->adapters[$nextAdapterIndex] : null;
  131. foreach ($items as $k => $item) {
  132. if (!$nextAdapter || $item->isHit()) {
  133. yield $k => $item;
  134. } else {
  135. $missing[] = $k;
  136. $misses[$k] = $item;
  137. }
  138. }
  139. if ($missing) {
  140. $syncItem = $this->syncItem;
  141. $adapter = $this->adapters[$adapterIndex];
  142. $items = $this->generateItems($nextAdapter->getItems($missing), $nextAdapterIndex);
  143. foreach ($items as $k => $item) {
  144. if ($item->isHit()) {
  145. $adapter->save($syncItem($item, $misses[$k]));
  146. }
  147. yield $k => $item;
  148. }
  149. }
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function hasItem($key)
  155. {
  156. foreach ($this->adapters as $adapter) {
  157. if ($adapter->hasItem($key)) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function clear()
  167. {
  168. $cleared = true;
  169. $i = $this->adapterCount;
  170. while ($i--) {
  171. $cleared = $this->adapters[$i]->clear() && $cleared;
  172. }
  173. return $cleared;
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function deleteItem($key)
  179. {
  180. $deleted = true;
  181. $i = $this->adapterCount;
  182. while ($i--) {
  183. $deleted = $this->adapters[$i]->deleteItem($key) && $deleted;
  184. }
  185. return $deleted;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function deleteItems(array $keys)
  191. {
  192. $deleted = true;
  193. $i = $this->adapterCount;
  194. while ($i--) {
  195. $deleted = $this->adapters[$i]->deleteItems($keys) && $deleted;
  196. }
  197. return $deleted;
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function save(CacheItemInterface $item)
  203. {
  204. $saved = true;
  205. $i = $this->adapterCount;
  206. while ($i--) {
  207. $saved = $this->adapters[$i]->save($item) && $saved;
  208. }
  209. return $saved;
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function saveDeferred(CacheItemInterface $item)
  215. {
  216. $saved = true;
  217. $i = $this->adapterCount;
  218. while ($i--) {
  219. $saved = $this->adapters[$i]->saveDeferred($item) && $saved;
  220. }
  221. return $saved;
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function commit()
  227. {
  228. $committed = true;
  229. $i = $this->adapterCount;
  230. while ($i--) {
  231. $committed = $this->adapters[$i]->commit() && $committed;
  232. }
  233. return $committed;
  234. }
  235. /**
  236. * {@inheritdoc}
  237. */
  238. public function prune()
  239. {
  240. $pruned = true;
  241. foreach ($this->adapters as $adapter) {
  242. if ($adapter instanceof PruneableInterface) {
  243. $pruned = $adapter->prune() && $pruned;
  244. }
  245. }
  246. return $pruned;
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. public function reset()
  252. {
  253. foreach ($this->adapters as $adapter) {
  254. if ($adapter instanceof ResetInterface) {
  255. $adapter->reset();
  256. }
  257. }
  258. }
  259. }