Redis.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Stefan Weil <sw@weilnetz.de>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Memcache;
  27. use OCP\IMemcacheTTL;
  28. class Redis extends Cache implements IMemcacheTTL {
  29. /**
  30. * @var \Redis $cache
  31. */
  32. private static $cache = null;
  33. public function __construct($prefix = '') {
  34. parent::__construct($prefix);
  35. if (is_null(self::$cache)) {
  36. self::$cache = \OC::$server->getGetRedisFactory()->getInstance();
  37. }
  38. }
  39. /**
  40. * entries in redis get namespaced to prevent collisions between ownCloud instances and users
  41. */
  42. protected function getNameSpace() {
  43. return $this->prefix;
  44. }
  45. public function get($key) {
  46. $result = self::$cache->get($this->getNamespace() . $key);
  47. if ($result === false && !self::$cache->exists($this->getNamespace() . $key)) {
  48. return null;
  49. } else {
  50. return json_decode($result, true);
  51. }
  52. }
  53. public function set($key, $value, $ttl = 0) {
  54. if ($ttl > 0) {
  55. return self::$cache->setex($this->getNamespace() . $key, $ttl, json_encode($value));
  56. } else {
  57. return self::$cache->set($this->getNamespace() . $key, json_encode($value));
  58. }
  59. }
  60. public function hasKey($key) {
  61. return self::$cache->exists($this->getNamespace() . $key);
  62. }
  63. public function remove($key) {
  64. if (self::$cache->delete($this->getNamespace() . $key)) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. }
  70. public function clear($prefix = '') {
  71. $prefix = $this->getNamespace() . $prefix . '*';
  72. $it = null;
  73. self::$cache->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
  74. while ($keys = self::$cache->scan($it, $prefix)) {
  75. self::$cache->delete($keys);
  76. }
  77. return true;
  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) ? false : true;
  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) ? false : true;
  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. static public function isAvailable() {
  161. return \OC::$server->getGetRedisFactory()->isAvailable();
  162. }
  163. }