1
0

CappedMemoryCache.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * @template-implements \ArrayAccess<string,T>
  32. */
  33. class CappedMemoryCache implements ICache, \ArrayAccess {
  34. private int $capacity;
  35. /** @var T[] */
  36. private array $cache = [];
  37. /**
  38. * @inheritdoc
  39. * @since 25.0.0
  40. */
  41. public function __construct(int $capacity = 512) {
  42. $this->capacity = $capacity;
  43. }
  44. /**
  45. * @inheritdoc
  46. * @since 25.0.0
  47. */
  48. public function hasKey($key): bool {
  49. return isset($this->cache[$key]);
  50. }
  51. /**
  52. * @return ?T
  53. * @since 25.0.0
  54. */
  55. public function get($key) {
  56. return $this->cache[$key] ?? null;
  57. }
  58. /**
  59. * @inheritdoc
  60. * @param string $key
  61. * @param T $value
  62. * @param int $ttl
  63. * @since 25.0.0
  64. * @return bool
  65. */
  66. public function set($key, $value, $ttl = 0): bool {
  67. if (is_null($key)) {
  68. $this->cache[] = $value;
  69. } else {
  70. $this->cache[$key] = $value;
  71. }
  72. $this->garbageCollect();
  73. return true;
  74. }
  75. /**
  76. * @since 25.0.0
  77. */
  78. public function remove($key): bool {
  79. unset($this->cache[$key]);
  80. return true;
  81. }
  82. /**
  83. * @inheritdoc
  84. * @since 25.0.0
  85. */
  86. public function clear($prefix = ''): bool {
  87. $this->cache = [];
  88. return true;
  89. }
  90. /**
  91. * @since 25.0.0
  92. */
  93. public function offsetExists($offset): bool {
  94. return $this->hasKey($offset);
  95. }
  96. /**
  97. * @inheritdoc
  98. * @return T
  99. * @since 25.0.0
  100. */
  101. #[\ReturnTypeWillChange]
  102. public function &offsetGet($offset) {
  103. return $this->cache[$offset];
  104. }
  105. /**
  106. * @inheritdoc
  107. * @param string $offset
  108. * @param T $value
  109. * @since 25.0.0
  110. */
  111. public function offsetSet($offset, $value): void {
  112. $this->set($offset, $value);
  113. }
  114. /**
  115. * @inheritdoc
  116. * @since 25.0.0
  117. */
  118. public function offsetUnset($offset): void {
  119. $this->remove($offset);
  120. }
  121. /**
  122. * @return T[]
  123. * @since 25.0.0
  124. */
  125. public function getData(): array {
  126. return $this->cache;
  127. }
  128. /**
  129. * @since 25.0.0
  130. */
  131. private function garbageCollect(): void {
  132. while (count($this->cache) > $this->capacity) {
  133. reset($this->cache);
  134. $key = key($this->cache);
  135. $this->remove($key);
  136. }
  137. }
  138. /**
  139. * @inheritdoc
  140. * @since 25.0.0
  141. */
  142. public static function isAvailable(): bool {
  143. return true;
  144. }
  145. }