ChainAdapter.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 $defaultLifetime;
  34. private static $syncItem;
  35. /**
  36. * @param CacheItemPoolInterface[] $adapters The ordered list of adapters used to fetch cached items
  37. * @param int $defaultLifetime The default lifetime of items propagated from lower adapters to upper ones
  38. */
  39. public function __construct(array $adapters, int $defaultLifetime = 0)
  40. {
  41. if (!$adapters) {
  42. throw new InvalidArgumentException('At least one adapter must be specified.');
  43. }
  44. foreach ($adapters as $adapter) {
  45. if (!$adapter instanceof CacheItemPoolInterface) {
  46. throw new InvalidArgumentException(sprintf('The class "%s" does not implement the "%s" interface.', get_debug_type($adapter), CacheItemPoolInterface::class));
  47. }
  48. if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && $adapter instanceof ApcuAdapter && !filter_var(ini_get('apc.enable_cli'), \FILTER_VALIDATE_BOOLEAN)) {
  49. continue; // skip putting APCu in the chain when the backend is disabled
  50. }
  51. if ($adapter instanceof AdapterInterface) {
  52. $this->adapters[] = $adapter;
  53. } else {
  54. $this->adapters[] = new ProxyAdapter($adapter);
  55. }
  56. }
  57. $this->adapterCount = \count($this->adapters);
  58. $this->defaultLifetime = $defaultLifetime;
  59. self::$syncItem ?? self::$syncItem = \Closure::bind(
  60. static function ($sourceItem, $item, $defaultLifetime, $sourceMetadata = null) {
  61. $sourceItem->isTaggable = false;
  62. $sourceMetadata = $sourceMetadata ?? $sourceItem->metadata;
  63. unset($sourceMetadata[CacheItem::METADATA_TAGS]);
  64. $item->value = $sourceItem->value;
  65. $item->isHit = $sourceItem->isHit;
  66. $item->metadata = $item->newMetadata = $sourceItem->metadata = $sourceMetadata;
  67. if (isset($item->metadata[CacheItem::METADATA_EXPIRY])) {
  68. $item->expiresAt(\DateTime::createFromFormat('U.u', sprintf('%.6F', $item->metadata[CacheItem::METADATA_EXPIRY])));
  69. } elseif (0 < $defaultLifetime) {
  70. $item->expiresAfter($defaultLifetime);
  71. }
  72. return $item;
  73. },
  74. null,
  75. CacheItem::class
  76. );
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
  82. {
  83. $lastItem = null;
  84. $i = 0;
  85. $wrap = function (CacheItem $item = null) use ($key, $callback, $beta, &$wrap, &$i, &$lastItem, &$metadata) {
  86. $adapter = $this->adapters[$i];
  87. if (isset($this->adapters[++$i])) {
  88. $callback = $wrap;
  89. $beta = \INF === $beta ? \INF : 0;
  90. }
  91. if ($adapter instanceof CacheInterface) {
  92. $value = $adapter->get($key, $callback, $beta, $metadata);
  93. } else {
  94. $value = $this->doGet($adapter, $key, $callback, $beta, $metadata);
  95. }
  96. if (null !== $item) {
  97. (self::$syncItem)($lastItem = $lastItem ?? $item, $item, $this->defaultLifetime, $metadata);
  98. }
  99. return $value;
  100. };
  101. return $wrap();
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function getItem($key)
  107. {
  108. $syncItem = self::$syncItem;
  109. $misses = [];
  110. foreach ($this->adapters as $i => $adapter) {
  111. $item = $adapter->getItem($key);
  112. if ($item->isHit()) {
  113. while (0 <= --$i) {
  114. $this->adapters[$i]->save($syncItem($item, $misses[$i], $this->defaultLifetime));
  115. }
  116. return $item;
  117. }
  118. $misses[$i] = $item;
  119. }
  120. return $item;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getItems(array $keys = [])
  126. {
  127. return $this->generateItems($this->adapters[0]->getItems($keys), 0);
  128. }
  129. private function generateItems(iterable $items, int $adapterIndex): \Generator
  130. {
  131. $missing = [];
  132. $misses = [];
  133. $nextAdapterIndex = $adapterIndex + 1;
  134. $nextAdapter = $this->adapters[$nextAdapterIndex] ?? null;
  135. foreach ($items as $k => $item) {
  136. if (!$nextAdapter || $item->isHit()) {
  137. yield $k => $item;
  138. } else {
  139. $missing[] = $k;
  140. $misses[$k] = $item;
  141. }
  142. }
  143. if ($missing) {
  144. $syncItem = self::$syncItem;
  145. $adapter = $this->adapters[$adapterIndex];
  146. $items = $this->generateItems($nextAdapter->getItems($missing), $nextAdapterIndex);
  147. foreach ($items as $k => $item) {
  148. if ($item->isHit()) {
  149. $adapter->save($syncItem($item, $misses[$k], $this->defaultLifetime));
  150. }
  151. yield $k => $item;
  152. }
  153. }
  154. }
  155. /**
  156. * {@inheritdoc}
  157. *
  158. * @return bool
  159. */
  160. public function hasItem($key)
  161. {
  162. foreach ($this->adapters as $adapter) {
  163. if ($adapter->hasItem($key)) {
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. /**
  170. * {@inheritdoc}
  171. *
  172. * @return bool
  173. */
  174. public function clear(string $prefix = '')
  175. {
  176. $cleared = true;
  177. $i = $this->adapterCount;
  178. while ($i--) {
  179. if ($this->adapters[$i] instanceof AdapterInterface) {
  180. $cleared = $this->adapters[$i]->clear($prefix) && $cleared;
  181. } else {
  182. $cleared = $this->adapters[$i]->clear() && $cleared;
  183. }
  184. }
  185. return $cleared;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. *
  190. * @return bool
  191. */
  192. public function deleteItem($key)
  193. {
  194. $deleted = true;
  195. $i = $this->adapterCount;
  196. while ($i--) {
  197. $deleted = $this->adapters[$i]->deleteItem($key) && $deleted;
  198. }
  199. return $deleted;
  200. }
  201. /**
  202. * {@inheritdoc}
  203. *
  204. * @return bool
  205. */
  206. public function deleteItems(array $keys)
  207. {
  208. $deleted = true;
  209. $i = $this->adapterCount;
  210. while ($i--) {
  211. $deleted = $this->adapters[$i]->deleteItems($keys) && $deleted;
  212. }
  213. return $deleted;
  214. }
  215. /**
  216. * {@inheritdoc}
  217. *
  218. * @return bool
  219. */
  220. public function save(CacheItemInterface $item)
  221. {
  222. $saved = true;
  223. $i = $this->adapterCount;
  224. while ($i--) {
  225. $saved = $this->adapters[$i]->save($item) && $saved;
  226. }
  227. return $saved;
  228. }
  229. /**
  230. * {@inheritdoc}
  231. *
  232. * @return bool
  233. */
  234. public function saveDeferred(CacheItemInterface $item)
  235. {
  236. $saved = true;
  237. $i = $this->adapterCount;
  238. while ($i--) {
  239. $saved = $this->adapters[$i]->saveDeferred($item) && $saved;
  240. }
  241. return $saved;
  242. }
  243. /**
  244. * {@inheritdoc}
  245. *
  246. * @return bool
  247. */
  248. public function commit()
  249. {
  250. $committed = true;
  251. $i = $this->adapterCount;
  252. while ($i--) {
  253. $committed = $this->adapters[$i]->commit() && $committed;
  254. }
  255. return $committed;
  256. }
  257. /**
  258. * {@inheritdoc}
  259. */
  260. public function prune()
  261. {
  262. $pruned = true;
  263. foreach ($this->adapters as $adapter) {
  264. if ($adapter instanceof PruneableInterface) {
  265. $pruned = $adapter->prune() && $pruned;
  266. }
  267. }
  268. return $pruned;
  269. }
  270. /**
  271. * {@inheritdoc}
  272. */
  273. public function reset()
  274. {
  275. foreach ($this->adapters as $adapter) {
  276. if ($adapter instanceof ResetInterface) {
  277. $adapter->reset();
  278. }
  279. }
  280. }
  281. }