ArrayCache.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * Array cache driver.
  22. *
  23. * @link www.doctrine-project.org
  24. * @since 2.0
  25. * @author Benjamin Eberlei <kontakt@beberlei.de>
  26. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  27. * @author Jonathan Wage <jonwage@gmail.com>
  28. * @author Roman Borschel <roman@code-factory.org>
  29. * @author David Abdemoulaie <dave@hobodave.com>
  30. */
  31. class ArrayCache extends CacheProvider
  32. {
  33. /**
  34. * @var array[] $data each element being a tuple of [$data, $expiration], where the expiration is int|bool
  35. */
  36. private $data = [];
  37. /**
  38. * @var int
  39. */
  40. private $hitsCount = 0;
  41. /**
  42. * @var int
  43. */
  44. private $missesCount = 0;
  45. /**
  46. * @var int
  47. */
  48. private $upTime;
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function __construct()
  53. {
  54. $this->upTime = time();
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function doFetch($id)
  60. {
  61. if (! $this->doContains($id)) {
  62. $this->missesCount += 1;
  63. return false;
  64. }
  65. $this->hitsCount += 1;
  66. return $this->data[$id][0];
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function doContains($id)
  72. {
  73. if (! isset($this->data[$id])) {
  74. return false;
  75. }
  76. $expiration = $this->data[$id][1];
  77. if ($expiration && $expiration < time()) {
  78. $this->doDelete($id);
  79. return false;
  80. }
  81. return true;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. protected function doSave($id, $data, $lifeTime = 0)
  87. {
  88. $this->data[$id] = [$data, $lifeTime ? time() + $lifeTime : false];
  89. return true;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. protected function doDelete($id)
  95. {
  96. unset($this->data[$id]);
  97. return true;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. protected function doFlush()
  103. {
  104. $this->data = [];
  105. return true;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. protected function doGetStats()
  111. {
  112. return [
  113. Cache::STATS_HITS => $this->hitsCount,
  114. Cache::STATS_MISSES => $this->missesCount,
  115. Cache::STATS_UPTIME => $this->upTime,
  116. Cache::STATS_MEMORY_USAGE => null,
  117. Cache::STATS_MEMORY_AVAILABLE => null,
  118. ];
  119. }
  120. }