ArrayCache.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace React\Cache;
  3. use React\Promise;
  4. use React\Promise\PromiseInterface;
  5. class ArrayCache implements CacheInterface
  6. {
  7. private $limit;
  8. private $data = array();
  9. private $expires = array();
  10. private $supportsHighResolution;
  11. /**
  12. * The `ArrayCache` provides an in-memory implementation of the [`CacheInterface`](#cacheinterface).
  13. *
  14. * ```php
  15. * $cache = new ArrayCache();
  16. *
  17. * $cache->set('foo', 'bar');
  18. * ```
  19. *
  20. * Its constructor accepts an optional `?int $limit` parameter to limit the
  21. * maximum number of entries to store in the LRU cache. If you add more
  22. * entries to this instance, it will automatically take care of removing
  23. * the one that was least recently used (LRU).
  24. *
  25. * For example, this snippet will overwrite the first value and only store
  26. * the last two entries:
  27. *
  28. * ```php
  29. * $cache = new ArrayCache(2);
  30. *
  31. * $cache->set('foo', '1');
  32. * $cache->set('bar', '2');
  33. * $cache->set('baz', '3');
  34. * ```
  35. *
  36. * This cache implementation is known to rely on wall-clock time to schedule
  37. * future cache expiration times when using any version before PHP 7.3,
  38. * because a monotonic time source is only available as of PHP 7.3 (`hrtime()`).
  39. * While this does not affect many common use cases, this is an important
  40. * distinction for programs that rely on a high time precision or on systems
  41. * that are subject to discontinuous time adjustments (time jumps).
  42. * This means that if you store a cache item with a TTL of 30s on PHP < 7.3
  43. * and then adjust your system time forward by 20s, the cache item may
  44. * expire in 10s. See also [`set()`](#set) for more details.
  45. *
  46. * @param int|null $limit maximum number of entries to store in the LRU cache
  47. */
  48. public function __construct($limit = null)
  49. {
  50. $this->limit = $limit;
  51. // prefer high-resolution timer, available as of PHP 7.3+
  52. $this->supportsHighResolution = \function_exists('hrtime');
  53. }
  54. public function get($key, $default = null)
  55. {
  56. // delete key if it is already expired => below will detect this as a cache miss
  57. if (isset($this->expires[$key]) && $this->now() - $this->expires[$key] > 0) {
  58. unset($this->data[$key], $this->expires[$key]);
  59. }
  60. if (!\array_key_exists($key, $this->data)) {
  61. return Promise\resolve($default);
  62. }
  63. // remove and append to end of array to keep track of LRU info
  64. $value = $this->data[$key];
  65. unset($this->data[$key]);
  66. $this->data[$key] = $value;
  67. return Promise\resolve($value);
  68. }
  69. public function set($key, $value, $ttl = null)
  70. {
  71. // unset before setting to ensure this entry will be added to end of array (LRU info)
  72. unset($this->data[$key]);
  73. $this->data[$key] = $value;
  74. // sort expiration times if TTL is given (first will expire first)
  75. unset($this->expires[$key]);
  76. if ($ttl !== null) {
  77. $this->expires[$key] = $this->now() + $ttl;
  78. \asort($this->expires);
  79. }
  80. // ensure size limit is not exceeded or remove first entry from array
  81. if ($this->limit !== null && \count($this->data) > $this->limit) {
  82. // first try to check if there's any expired entry
  83. // expiration times are sorted, so we can simply look at the first one
  84. \reset($this->expires);
  85. $key = \key($this->expires);
  86. // check to see if the first in the list of expiring keys is already expired
  87. // if the first key is not expired, we have to overwrite by using LRU info
  88. if ($key === null || $this->now() - $this->expires[$key] < 0) {
  89. \reset($this->data);
  90. $key = \key($this->data);
  91. }
  92. unset($this->data[$key], $this->expires[$key]);
  93. }
  94. return Promise\resolve(true);
  95. }
  96. public function delete($key)
  97. {
  98. unset($this->data[$key], $this->expires[$key]);
  99. return Promise\resolve(true);
  100. }
  101. public function getMultiple(array $keys, $default = null)
  102. {
  103. $values = array();
  104. foreach ($keys as $key) {
  105. $values[$key] = $this->get($key, $default);
  106. }
  107. return Promise\all($values);
  108. }
  109. public function setMultiple(array $values, $ttl = null)
  110. {
  111. foreach ($values as $key => $value) {
  112. $this->set($key, $value, $ttl);
  113. }
  114. return Promise\resolve(true);
  115. }
  116. public function deleteMultiple(array $keys)
  117. {
  118. foreach ($keys as $key) {
  119. unset($this->data[$key], $this->expires[$key]);
  120. }
  121. return Promise\resolve(true);
  122. }
  123. public function clear()
  124. {
  125. $this->data = array();
  126. $this->expires = array();
  127. return Promise\resolve(true);
  128. }
  129. public function has($key)
  130. {
  131. // delete key if it is already expired
  132. if (isset($this->expires[$key]) && $this->now() - $this->expires[$key] > 0) {
  133. unset($this->data[$key], $this->expires[$key]);
  134. }
  135. if (!\array_key_exists($key, $this->data)) {
  136. return Promise\resolve(false);
  137. }
  138. // remove and append to end of array to keep track of LRU info
  139. $value = $this->data[$key];
  140. unset($this->data[$key]);
  141. $this->data[$key] = $value;
  142. return Promise\resolve(true);
  143. }
  144. /**
  145. * @return float
  146. */
  147. private function now()
  148. {
  149. return $this->supportsHighResolution ? \hrtime(true) * 1e-9 : \microtime(true);
  150. }
  151. }