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