1
0

ArrayCache.php 3.2 KB

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