ArrayCache.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Memcache;
  26. use OCP\IMemcache;
  27. class ArrayCache extends Cache implements IMemcache {
  28. /** @var array Array with the cached data */
  29. protected $cachedData = [];
  30. use CADTrait;
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function get($key) {
  35. if ($this->hasKey($key)) {
  36. return $this->cachedData[$key];
  37. }
  38. return null;
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function set($key, $value, $ttl = 0) {
  44. $this->cachedData[$key] = $value;
  45. return true;
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function hasKey($key) {
  51. return isset($this->cachedData[$key]);
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function remove($key) {
  57. unset($this->cachedData[$key]);
  58. return true;
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function clear($prefix = '') {
  64. if ($prefix === '') {
  65. $this->cachedData = [];
  66. return true;
  67. }
  68. foreach ($this->cachedData as $key => $value) {
  69. if (strpos($key, $prefix) === 0) {
  70. $this->remove($key);
  71. }
  72. }
  73. return true;
  74. }
  75. /**
  76. * Set a value in the cache if it's not already stored
  77. *
  78. * @param string $key
  79. * @param mixed $value
  80. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  81. * @return bool
  82. */
  83. public function add($key, $value, $ttl = 0) {
  84. // since this cache is not shared race conditions aren't an issue
  85. if ($this->hasKey($key)) {
  86. return false;
  87. } else {
  88. return $this->set($key, $value, $ttl);
  89. }
  90. }
  91. /**
  92. * Increase a stored number
  93. *
  94. * @param string $key
  95. * @param int $step
  96. * @return int | bool
  97. */
  98. public function inc($key, $step = 1) {
  99. $oldValue = $this->get($key);
  100. if (is_int($oldValue)) {
  101. $this->set($key, $oldValue + $step);
  102. return $oldValue + $step;
  103. } else {
  104. $success = $this->add($key, $step);
  105. return $success ? $step : false;
  106. }
  107. }
  108. /**
  109. * Decrease a stored number
  110. *
  111. * @param string $key
  112. * @param int $step
  113. * @return int | bool
  114. */
  115. public function dec($key, $step = 1) {
  116. $oldValue = $this->get($key);
  117. if (is_int($oldValue)) {
  118. $this->set($key, $oldValue - $step);
  119. return $oldValue - $step;
  120. } else {
  121. return false;
  122. }
  123. }
  124. /**
  125. * Compare and set
  126. *
  127. * @param string $key
  128. * @param mixed $old
  129. * @param mixed $new
  130. * @return bool
  131. */
  132. public function cas($key, $old, $new) {
  133. if ($this->get($key) === $old) {
  134. return $this->set($key, $new);
  135. } else {
  136. return false;
  137. }
  138. }
  139. /**
  140. * {@inheritDoc}
  141. */
  142. public static function isAvailable(): bool {
  143. return true;
  144. }
  145. }