ArrayCache.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use function time;
  4. /**
  5. * Array cache driver.
  6. *
  7. * @deprecated Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0
  8. *
  9. * @link www.doctrine-project.org
  10. */
  11. class ArrayCache extends CacheProvider
  12. {
  13. /** @psalm-var array<string, array{mixed, int|bool}>> $data each element being a tuple of [$data, $expiration], where the expiration is int|bool */
  14. private $data = [];
  15. /** @var int */
  16. private $hitsCount = 0;
  17. /** @var int */
  18. private $missesCount = 0;
  19. /** @var int */
  20. private $upTime;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function __construct()
  25. {
  26. $this->upTime = time();
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function doFetch($id)
  32. {
  33. if (! $this->doContains($id)) {
  34. $this->missesCount += 1;
  35. return false;
  36. }
  37. $this->hitsCount += 1;
  38. return $this->data[$id][0];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. protected function doContains($id)
  44. {
  45. if (! isset($this->data[$id])) {
  46. return false;
  47. }
  48. $expiration = $this->data[$id][1];
  49. if ($expiration && $expiration < time()) {
  50. $this->doDelete($id);
  51. return false;
  52. }
  53. return true;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function doSave($id, $data, $lifeTime = 0)
  59. {
  60. $this->data[$id] = [$data, $lifeTime ? time() + $lifeTime : false];
  61. return true;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. protected function doDelete($id)
  67. {
  68. unset($this->data[$id]);
  69. return true;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. protected function doFlush()
  75. {
  76. $this->data = [];
  77. return true;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. protected function doGetStats()
  83. {
  84. return [
  85. Cache::STATS_HITS => $this->hitsCount,
  86. Cache::STATS_MISSES => $this->missesCount,
  87. Cache::STATS_UPTIME => $this->upTime,
  88. Cache::STATS_MEMORY_USAGE => null,
  89. Cache::STATS_MEMORY_AVAILABLE => null,
  90. ];
  91. }
  92. }