You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.4 KiB

3 years ago
  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 Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Stefan Weil <sw@weilnetz.de>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Memcache;
  30. use OCP\IMemcacheTTL;
  31. class Redis extends Cache implements IMemcacheTTL {
  32. /**
  33. * @var \Redis $cache
  34. */
  35. private static $cache = null;
  36. public function __construct($prefix = '') {
  37. parent::__construct($prefix);
  38. if (is_null(self::$cache)) {
  39. self::$cache = \OC::$server->getGetRedisFactory()->getInstance();
  40. }
  41. }
  42. /**
  43. * entries in redis get namespaced to prevent collisions between ownCloud instances and users
  44. */
  45. protected function getNameSpace() {
  46. return $this->prefix;
  47. }
  48. public function get($key) {
  49. $result = self::$cache->get($this->getNameSpace() . $key);
  50. if ($result === false && !self::$cache->exists($this->getNameSpace() . $key)) {
  51. return null;
  52. } else {
  53. return json_decode($result, true);
  54. }
  55. }
  56. public function set($key, $value, $ttl = 0) {
  57. if ($ttl > 0) {
  58. return self::$cache->setex($this->getNameSpace() . $key, $ttl, json_encode($value));
  59. } else {
  60. return self::$cache->set($this->getNameSpace() . $key, json_encode($value));
  61. }
  62. }
  63. public function hasKey($key) {
  64. return self::$cache->exists($this->getNameSpace() . $key);
  65. }
  66. public function remove($key) {
  67. if (self::$cache->del($this->getNameSpace() . $key)) {
  68. return true;
  69. } else {
  70. return false;
  71. }
  72. }
  73. public function clear($prefix = '') {
  74. $prefix = $this->getNameSpace() . $prefix . '*';
  75. $keys = self::$cache->keys($prefix);
  76. $deleted = self::$cache->del($keys);
  77. return count($keys) === $deleted;
  78. }
  79. /**
  80. * Set a value in the cache if it's not already stored
  81. *
  82. * @param string $key
  83. * @param mixed $value
  84. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  85. * @return bool
  86. */
  87. public function add($key, $value, $ttl = 0) {
  88. // don't encode ints for inc/dec
  89. if (!is_int($value)) {
  90. $value = json_encode($value);
  91. }
  92. return self::$cache->setnx($this->getPrefix() . $key, $value);
  93. }
  94. /**
  95. * Increase a stored number
  96. *
  97. * @param string $key
  98. * @param int $step
  99. * @return int | bool
  100. */
  101. public function inc($key, $step = 1) {
  102. return self::$cache->incrBy($this->getNameSpace() . $key, $step);
  103. }
  104. /**
  105. * Decrease a stored number
  106. *
  107. * @param string $key
  108. * @param int $step
  109. * @return int | bool
  110. */
  111. public function dec($key, $step = 1) {
  112. if (!$this->hasKey($key)) {
  113. return false;
  114. }
  115. return self::$cache->decrBy($this->getNameSpace() . $key, $step);
  116. }
  117. /**
  118. * Compare and set
  119. *
  120. * @param string $key
  121. * @param mixed $old
  122. * @param mixed $new
  123. * @return bool
  124. */
  125. public function cas($key, $old, $new) {
  126. if (!is_int($new)) {
  127. $new = json_encode($new);
  128. }
  129. self::$cache->watch($this->getNameSpace() . $key);
  130. if ($this->get($key) === $old) {
  131. $result = self::$cache->multi()
  132. ->set($this->getNameSpace() . $key, $new)
  133. ->exec();
  134. return $result !== false;
  135. }
  136. self::$cache->unwatch();
  137. return false;
  138. }
  139. /**
  140. * Compare and delete
  141. *
  142. * @param string $key
  143. * @param mixed $old
  144. * @return bool
  145. */
  146. public function cad($key, $old) {
  147. self::$cache->watch($this->getNameSpace() . $key);
  148. if ($this->get($key) === $old) {
  149. $result = self::$cache->multi()
  150. ->del($this->getNameSpace() . $key)
  151. ->exec();
  152. return $result !== false;
  153. }
  154. self::$cache->unwatch();
  155. return false;
  156. }
  157. public function setTTL($key, $ttl) {
  158. self::$cache->expire($this->getNameSpace() . $key, $ttl);
  159. }
  160. public static function isAvailable() {
  161. return \OC::$server->getGetRedisFactory()->isAvailable();
  162. }
  163. }