ChainAdapter.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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, $sourceMetadata = null) use ($defaultLifetime) {
  56. $sourceItem->isTaggable = false;
  57. $sourceMetadata = $sourceMetadata ?? $sourceItem->metadata;
  58. unset($sourceMetadata[CacheItem::METADATA_TAGS]);
  59. $item->value = $sourceItem->value;
  60. $item->expiry = $sourceMetadata[CacheItem::METADATA_EXPIRY] ?? $sourceItem->expiry;
  61. $item->isHit = $sourceItem->isHit;
  62. $item->metadata = $item->newMetadata = $sourceItem->metadata = $sourceMetadata;
  63. if (0 < $sourceItem->defaultLifetime && $sourceItem->defaultLifetime < $defaultLifetime) {
  64. $defaultLifetime = $sourceItem->defaultLifetime;
  65. }
  66. if (0 < $defaultLifetime && ($item->defaultLifetime <= 0 || $defaultLifetime < $item->defaultLifetime)) {
  67. $item->defaultLifetime = $defaultLifetime;
  68. }
  69. return $item;
  70. },
  71. null,
  72. CacheItem::class
  73. );
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
  79. {
  80. $lastItem = null;
  81. $i = 0;
  82. $wrap = function (CacheItem $item = null) use ($key, $callback, $beta, &$wrap, &$i, &$lastItem, &$metadata) {
  83. $adapter = $this->adapters[$i];
  84. if (isset($this->adapters[++$i])) {
  85. $callback = $wrap;
  86. $beta = INF === $beta ? INF : 0;
  87. }
  88. if ($adapter instanceof CacheInterface) {
  89. $value = $adapter->get($key, $callback, $beta, $metadata);
  90. } else {
  91. $value = $this->doGet($adapter, $key, $callback, $beta, $metadata);
  92. }
  93. if (null !== $item) {
  94. ($this->syncItem)($lastItem = $lastItem ?? $item, $item, $metadata);
  95. }
  96. return $value;
  97. };
  98. return $wrap();
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function getItem($key)
  104. {
  105. $syncItem = $this->syncItem;
  106. $misses = [];
  107. foreach ($this->adapters as $i => $adapter) {
  108. $item = $adapter->getItem($key);
  109. if ($item->isHit()) {
  110. while (0 <= --$i) {
  111. $this->adapters[$i]->save($syncItem($item, $misses[$i]));
  112. }
  113. return $item;
  114. }
  115. $misses[$i] = $item;
  116. }
  117. return $item;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function getItems(array $keys = [])
  123. {
  124. return $this->generateItems($this->adapters[0]->getItems($keys), 0);
  125. }
  126. private function generateItems(iterable $items, int $adapterIndex)
  127. {
  128. $missing = [];
  129. $misses = [];
  130. $nextAdapterIndex = $adapterIndex + 1;
  131. $nextAdapter = isset($this->adapters[$nextAdapterIndex]) ? $this->adapters[$nextAdapterIndex] : null;
  132. foreach ($items as $k => $item) {
  133. if (!$nextAdapter || $item->isHit()) {
  134. yield $k => $item;
  135. } else {
  136. $missing[] = $k;
  137. $misses[$k] = $item;
  138. }
  139. }
  140. if ($missing) {
  141. $syncItem = $this->syncItem;
  142. $adapter = $this->adapters[$adapterIndex];
  143. $items = $this->generateItems($nextAdapter->getItems($missing), $nextAdapterIndex);
  144. foreach ($items as $k => $item) {
  145. if ($item->isHit()) {
  146. $adapter->save($syncItem($item, $misses[$k]));
  147. }
  148. yield $k => $item;
  149. }
  150. }
  151. }
  152. /**
  153. * {@inheritdoc}
  154. *
  155. * @return bool
  156. */
  157. public function hasItem($key)
  158. {
  159. foreach ($this->adapters as $adapter) {
  160. if ($adapter->hasItem($key)) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. /**
  167. * {@inheritdoc}
  168. *
  169. * @return bool
  170. */
  171. public function clear(string $prefix = '')
  172. {
  173. $cleared = true;
  174. $i = $this->adapterCount;
  175. while ($i--) {
  176. if ($this->adapters[$i] instanceof AdapterInterface) {
  177. $cleared = $this->adapters[$i]->clear($prefix) && $cleared;
  178. } else {
  179. $cleared = $this->adapters[$i]->clear() && $cleared;
  180. }
  181. }
  182. return $cleared;
  183. }
  184. /**
  185. * {@inheritdoc}
  186. *
  187. * @return bool
  188. */
  189. public function deleteItem($key)
  190. {
  191. $deleted = true;
  192. $i = $this->adapterCount;
  193. while ($i--) {
  194. $deleted = $this->adapters[$i]->deleteItem($key) && $deleted;
  195. }
  196. return $deleted;
  197. }
  198. /**
  199. * {@inheritdoc}
  200. *
  201. * @return bool
  202. */
  203. public function deleteItems(array $keys)
  204. {
  205. $deleted = true;
  206. $i = $this->adapterCount;
  207. while ($i--) {
  208. $deleted = $this->adapters[$i]->deleteItems($keys) && $deleted;
  209. }
  210. return $deleted;
  211. }
  212. /**
  213. * {@inheritdoc}
  214. *
  215. * @return bool
  216. */
  217. public function save(CacheItemInterface $item)
  218. {
  219. $saved = true;
  220. $i = $this->adapterCount;
  221. while ($i--) {
  222. $saved = $this->adapters[$i]->save($item) && $saved;
  223. }
  224. return $saved;
  225. }
  226. /**
  227. * {@inheritdoc}
  228. *
  229. * @return bool
  230. */
  231. public function saveDeferred(CacheItemInterface $item)
  232. {
  233. $saved = true;
  234. $i = $this->adapterCount;
  235. while ($i--) {
  236. $saved = $this->adapters[$i]->saveDeferred($item) && $saved;
  237. }
  238. return $saved;
  239. }
  240. /**
  241. * {@inheritdoc}
  242. *
  243. * @return bool
  244. */
  245. public function commit()
  246. {
  247. $committed = true;
  248. $i = $this->adapterCount;
  249. while ($i--) {
  250. $committed = $this->adapters[$i]->commit() && $committed;
  251. }
  252. return $committed;
  253. }
  254. /**
  255. * {@inheritdoc}
  256. */
  257. public function prune()
  258. {
  259. $pruned = true;
  260. foreach ($this->adapters as $adapter) {
  261. if ($adapter instanceof PruneableInterface) {
  262. $pruned = $adapter->prune() && $pruned;
  263. }
  264. }
  265. return $pruned;
  266. }
  267. /**
  268. * {@inheritdoc}
  269. */
  270. public function reset()
  271. {
  272. foreach ($this->adapters as $adapter) {
  273. if ($adapter instanceof ResetInterface) {
  274. $adapter->reset();
  275. }
  276. }
  277. }
  278. }