1
0

CappedMemoryCache.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 OC\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. * @template T
  29. * @deprecated use OCP\Cache\CappedMemoryCache instead
  30. */
  31. class CappedMemoryCache implements ICache, \ArrayAccess {
  32. private $capacity;
  33. /** @var T[] */
  34. private $cache = [];
  35. public function __construct($capacity = 512) {
  36. $this->capacity = $capacity;
  37. }
  38. public function hasKey($key): bool {
  39. return isset($this->cache[$key]);
  40. }
  41. /**
  42. * @return ?T
  43. */
  44. public function get($key) {
  45. return $this->cache[$key] ?? null;
  46. }
  47. /**
  48. * @param string $key
  49. * @param T $value
  50. * @param int $ttl
  51. * @return bool
  52. */
  53. public function set($key, $value, $ttl = 0): bool {
  54. if (is_null($key)) {
  55. $this->cache[] = $value;
  56. } else {
  57. $this->cache[$key] = $value;
  58. }
  59. $this->garbageCollect();
  60. return true;
  61. }
  62. public function remove($key) {
  63. unset($this->cache[$key]);
  64. return true;
  65. }
  66. public function clear($prefix = '') {
  67. $this->cache = [];
  68. return true;
  69. }
  70. public function offsetExists($offset): bool {
  71. return $this->hasKey($offset);
  72. }
  73. /**
  74. * @return T
  75. */
  76. #[\ReturnTypeWillChange]
  77. public function &offsetGet($offset) {
  78. return $this->cache[$offset];
  79. }
  80. /**
  81. * @param string $offset
  82. * @param T $value
  83. * @return void
  84. */
  85. public function offsetSet($offset, $value): void {
  86. $this->set($offset, $value);
  87. }
  88. public function offsetUnset($offset): void {
  89. $this->remove($offset);
  90. }
  91. /**
  92. * @return T[]
  93. */
  94. public function getData() {
  95. return $this->cache;
  96. }
  97. private function garbageCollect() {
  98. while (count($this->cache) > $this->capacity) {
  99. reset($this->cache);
  100. $key = key($this->cache);
  101. $this->remove($key);
  102. }
  103. }
  104. public static function isAvailable(): bool {
  105. return true;
  106. }
  107. }