CappedMemoryCache.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCP\Cache;
  23. use OCP\ICache;
  24. /**
  25. * In-memory cache with a capacity limit to keep memory usage in check
  26. *
  27. * Uses a simple FIFO expiry mechanism
  28. *
  29. * @since 25.0.0
  30. * @template T
  31. */
  32. class CappedMemoryCache implements ICache, \ArrayAccess {
  33. private int $capacity;
  34. /** @var T[] */
  35. private array $cache = [];
  36. /**
  37. * @inheritdoc
  38. * @since 25.0.0
  39. */
  40. public function __construct(int $capacity = 512) {
  41. $this->capacity = $capacity;
  42. }
  43. /**
  44. * @inheritdoc
  45. * @since 25.0.0
  46. */
  47. public function hasKey($key): bool {
  48. return isset($this->cache[$key]);
  49. }
  50. /**
  51. * @return ?T
  52. * @since 25.0.0
  53. */
  54. public function get($key) {
  55. return $this->cache[$key] ?? null;
  56. }
  57. /**
  58. * @inheritdoc
  59. * @param string $key
  60. * @param T $value
  61. * @param int $ttl
  62. * @since 25.0.0
  63. * @return bool
  64. */
  65. public function set($key, $value, $ttl = 0): bool {
  66. if (is_null($key)) {
  67. $this->cache[] = $value;
  68. } else {
  69. $this->cache[$key] = $value;
  70. }
  71. $this->garbageCollect();
  72. return true;
  73. }
  74. /**
  75. * @since 25.0.0
  76. */
  77. public function remove($key): bool {
  78. unset($this->cache[$key]);
  79. return true;
  80. }
  81. /**
  82. * @inheritdoc
  83. * @since 25.0.0
  84. */
  85. public function clear($prefix = ''): bool {
  86. $this->cache = [];
  87. return true;
  88. }
  89. /**
  90. * @since 25.0.0
  91. */
  92. public function offsetExists($offset): bool {
  93. return $this->hasKey($offset);
  94. }
  95. /**
  96. * @inheritdoc
  97. * @return T
  98. * @since 25.0.0
  99. */
  100. #[\ReturnTypeWillChange]
  101. public function &offsetGet($offset) {
  102. return $this->cache[$offset];
  103. }
  104. /**
  105. * @inheritdoc
  106. * @param string $offset
  107. * @param T $value
  108. * @since 25.0.0
  109. */
  110. public function offsetSet($offset, $value): void {
  111. $this->set($offset, $value);
  112. }
  113. /**
  114. * @inheritdoc
  115. * @since 25.0.0
  116. */
  117. public function offsetUnset($offset): void {
  118. $this->remove($offset);
  119. }
  120. /**
  121. * @return T[]
  122. * @since 25.0.0
  123. */
  124. public function getData(): array {
  125. return $this->cache;
  126. }
  127. /**
  128. * @since 25.0.0
  129. */
  130. private function garbageCollect(): void {
  131. while (count($this->cache) > $this->capacity) {
  132. reset($this->cache);
  133. $key = key($this->cache);
  134. $this->remove($key);
  135. }
  136. }
  137. /**
  138. * @inheritdoc
  139. * @since 25.0.0
  140. */
  141. public static function isAvailable(): bool {
  142. return true;
  143. }
  144. }