ChainCache.php 4.8 KB

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