ChainCache.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Cache;
  20. /**
  21. * Cache provider that allows to easily chain multiple cache providers
  22. *
  23. * @author Michaël Gallego <mic.gallego@gmail.com>
  24. */
  25. class ChainCache extends CacheProvider
  26. {
  27. /**
  28. * @var CacheProvider[]
  29. */
  30. private $cacheProviders = array();
  31. /**
  32. * Constructor
  33. *
  34. * @param CacheProvider[] $cacheProviders
  35. */
  36. public function __construct($cacheProviders = array())
  37. {
  38. $this->cacheProviders = $cacheProviders;
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function setNamespace($namespace)
  44. {
  45. parent::setNamespace($namespace);
  46. foreach ($this->cacheProviders as $cacheProvider) {
  47. $cacheProvider->setNamespace($namespace);
  48. }
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. protected function doFetch($id)
  54. {
  55. foreach ($this->cacheProviders as $key => $cacheProvider) {
  56. if ($cacheProvider->doContains($id)) {
  57. $value = $cacheProvider->doFetch($id);
  58. // We populate all the previous cache layers (that are assumed to be faster)
  59. for ($subKey = $key - 1 ; $subKey >= 0 ; $subKey--) {
  60. $this->cacheProviders[$subKey]->doSave($id, $value);
  61. }
  62. return $value;
  63. }
  64. }
  65. return false;
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. protected function doContains($id)
  71. {
  72. foreach ($this->cacheProviders as $cacheProvider) {
  73. if ($cacheProvider->doContains($id)) {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. protected function doSave($id, $data, $lifeTime = 0)
  83. {
  84. $stored = true;
  85. foreach ($this->cacheProviders as $cacheProvider) {
  86. $stored = $cacheProvider->doSave($id, $data, $lifeTime) && $stored;
  87. }
  88. return $stored;
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. protected function doDelete($id)
  94. {
  95. $deleted = true;
  96. foreach ($this->cacheProviders as $cacheProvider) {
  97. $deleted = $cacheProvider->doDelete($id) && $deleted;
  98. }
  99. return $deleted;
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. protected function doFlush()
  105. {
  106. $flushed = true;
  107. foreach ($this->cacheProviders as $cacheProvider) {
  108. $flushed = $cacheProvider->doFlush() && $flushed;
  109. }
  110. return $flushed;
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. protected function doGetStats()
  116. {
  117. // We return all the stats from all adapters
  118. $stats = array();
  119. foreach ($this->cacheProviders as $cacheProvider) {
  120. $stats[] = $cacheProvider->doGetStats();
  121. }
  122. return $stats;
  123. }
  124. }