1
0

ArrayCache.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Memcache;
  8. use OCP\IMemcache;
  9. class ArrayCache extends Cache implements IMemcache {
  10. /** @var array Array with the cached data */
  11. protected $cachedData = [];
  12. use CADTrait;
  13. /**
  14. * {@inheritDoc}
  15. */
  16. public function get($key) {
  17. if ($this->hasKey($key)) {
  18. return $this->cachedData[$key];
  19. }
  20. return null;
  21. }
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function set($key, $value, $ttl = 0) {
  26. $this->cachedData[$key] = $value;
  27. return true;
  28. }
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function hasKey($key) {
  33. return isset($this->cachedData[$key]);
  34. }
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function remove($key) {
  39. unset($this->cachedData[$key]);
  40. return true;
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function clear($prefix = '') {
  46. if ($prefix === '') {
  47. $this->cachedData = [];
  48. return true;
  49. }
  50. foreach ($this->cachedData as $key => $value) {
  51. if (str_starts_with($key, $prefix)) {
  52. $this->remove($key);
  53. }
  54. }
  55. return true;
  56. }
  57. /**
  58. * Set a value in the cache if it's not already stored
  59. *
  60. * @param string $key
  61. * @param mixed $value
  62. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  63. * @return bool
  64. */
  65. public function add($key, $value, $ttl = 0) {
  66. // since this cache is not shared race conditions aren't an issue
  67. if ($this->hasKey($key)) {
  68. return false;
  69. } else {
  70. return $this->set($key, $value, $ttl);
  71. }
  72. }
  73. /**
  74. * Increase a stored number
  75. *
  76. * @param string $key
  77. * @param int $step
  78. * @return int | bool
  79. */
  80. public function inc($key, $step = 1) {
  81. $oldValue = $this->get($key);
  82. if (is_int($oldValue)) {
  83. $this->set($key, $oldValue + $step);
  84. return $oldValue + $step;
  85. } else {
  86. $success = $this->add($key, $step);
  87. return $success ? $step : false;
  88. }
  89. }
  90. /**
  91. * Decrease a stored number
  92. *
  93. * @param string $key
  94. * @param int $step
  95. * @return int | bool
  96. */
  97. public function dec($key, $step = 1) {
  98. $oldValue = $this->get($key);
  99. if (is_int($oldValue)) {
  100. $this->set($key, $oldValue - $step);
  101. return $oldValue - $step;
  102. } else {
  103. return false;
  104. }
  105. }
  106. /**
  107. * Compare and set
  108. *
  109. * @param string $key
  110. * @param mixed $old
  111. * @param mixed $new
  112. * @return bool
  113. */
  114. public function cas($key, $old, $new) {
  115. if ($this->get($key) === $old) {
  116. return $this->set($key, $new);
  117. } else {
  118. return false;
  119. }
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public static function isAvailable(): bool {
  125. return true;
  126. }
  127. }