ChainCache.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use function array_values;
  4. use function count;
  5. use function iterator_to_array;
  6. /**
  7. * Cache provider that allows to easily chain multiple cache providers
  8. */
  9. class ChainCache extends CacheProvider
  10. {
  11. /** @var CacheProvider[] */
  12. private $cacheProviders = [];
  13. /**
  14. * @param CacheProvider[] $cacheProviders
  15. */
  16. public function __construct($cacheProviders = [])
  17. {
  18. $this->cacheProviders = $cacheProviders instanceof \Traversable
  19. ? iterator_to_array($cacheProviders, false)
  20. : array_values($cacheProviders);
  21. }
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function setNamespace($namespace)
  26. {
  27. parent::setNamespace($namespace);
  28. foreach ($this->cacheProviders as $cacheProvider) {
  29. $cacheProvider->setNamespace($namespace);
  30. }
  31. }
  32. /**
  33. * {@inheritDoc}
  34. */
  35. protected function doFetch($id)
  36. {
  37. foreach ($this->cacheProviders as $key => $cacheProvider) {
  38. if ($cacheProvider->doContains($id)) {
  39. $value = $cacheProvider->doFetch($id);
  40. // We populate all the previous cache layers (that are assumed to be faster)
  41. for ($subKey = $key - 1; $subKey >= 0; $subKey--) {
  42. $this->cacheProviders[$subKey]->doSave($id, $value);
  43. }
  44. return $value;
  45. }
  46. }
  47. return false;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function doFetchMultiple(array $keys)
  53. {
  54. /** @var CacheProvider[] $traversedProviders */
  55. $traversedProviders = [];
  56. $keysCount = count($keys);
  57. $fetchedValues = [];
  58. foreach ($this->cacheProviders as $key => $cacheProvider) {
  59. $fetchedValues = $cacheProvider->doFetchMultiple($keys);
  60. // We populate all the previous cache layers (that are assumed to be faster)
  61. if (count($fetchedValues) === $keysCount) {
  62. foreach ($traversedProviders as $previousCacheProvider) {
  63. $previousCacheProvider->doSaveMultiple($fetchedValues);
  64. }
  65. return $fetchedValues;
  66. }
  67. $traversedProviders[] = $cacheProvider;
  68. }
  69. return $fetchedValues;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. protected function doContains($id)
  75. {
  76. foreach ($this->cacheProviders as $cacheProvider) {
  77. if ($cacheProvider->doContains($id)) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. protected function doSave($id, $data, $lifeTime = 0)
  87. {
  88. $stored = true;
  89. foreach ($this->cacheProviders as $cacheProvider) {
  90. $stored = $cacheProvider->doSave($id, $data, $lifeTime) && $stored;
  91. }
  92. return $stored;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
  98. {
  99. $stored = true;
  100. foreach ($this->cacheProviders as $cacheProvider) {
  101. $stored = $cacheProvider->doSaveMultiple($keysAndValues, $lifetime) && $stored;
  102. }
  103. return $stored;
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. protected function doDelete($id)
  109. {
  110. $deleted = true;
  111. foreach ($this->cacheProviders as $cacheProvider) {
  112. $deleted = $cacheProvider->doDelete($id) && $deleted;
  113. }
  114. return $deleted;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. protected function doDeleteMultiple(array $keys)
  120. {
  121. $deleted = true;
  122. foreach ($this->cacheProviders as $cacheProvider) {
  123. $deleted = $cacheProvider->doDeleteMultiple($keys) && $deleted;
  124. }
  125. return $deleted;
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. protected function doFlush()
  131. {
  132. $flushed = true;
  133. foreach ($this->cacheProviders as $cacheProvider) {
  134. $flushed = $cacheProvider->doFlush() && $flushed;
  135. }
  136. return $flushed;
  137. }
  138. /**
  139. * {@inheritDoc}
  140. */
  141. protected function doGetStats()
  142. {
  143. // We return all the stats from all adapters
  144. $stats = [];
  145. foreach ($this->cacheProviders as $cacheProvider) {
  146. $stats[] = $cacheProvider->doGetStats();
  147. }
  148. return $stats;
  149. }
  150. }