Redis.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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->del($this->getNameSpace() . $key)) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. }
  70. public function clear($prefix = '') {
  71. $prefix = $this->getNameSpace() . $prefix . '*';
  72. $keys = self::$cache->keys($prefix);
  73. $deleted = self::$cache->del($keys);
  74. return count($keys) === $deleted;
  75. }
  76. /**
  77. * Set a value in the cache if it's not already stored
  78. *
  79. * @param string $key
  80. * @param mixed $value
  81. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  82. * @return bool
  83. */
  84. public function add($key, $value, $ttl = 0) {
  85. // don't encode ints for inc/dec
  86. if (!is_int($value)) {
  87. $value = json_encode($value);
  88. }
  89. return self::$cache->setnx($this->getPrefix() . $key, $value);
  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. return self::$cache->incrBy($this->getNameSpace() . $key, $step);
  100. }
  101. /**
  102. * Decrease a stored number
  103. *
  104. * @param string $key
  105. * @param int $step
  106. * @return int | bool
  107. */
  108. public function dec($key, $step = 1) {
  109. if (!$this->hasKey($key)) {
  110. return false;
  111. }
  112. return self::$cache->decrBy($this->getNameSpace() . $key, $step);
  113. }
  114. /**
  115. * Compare and set
  116. *
  117. * @param string $key
  118. * @param mixed $old
  119. * @param mixed $new
  120. * @return bool
  121. */
  122. public function cas($key, $old, $new) {
  123. if (!is_int($new)) {
  124. $new = json_encode($new);
  125. }
  126. self::$cache->watch($this->getNameSpace() . $key);
  127. if ($this->get($key) === $old) {
  128. $result = self::$cache->multi()
  129. ->set($this->getNameSpace() . $key, $new)
  130. ->exec();
  131. return ($result === false) ? false : true;
  132. }
  133. self::$cache->unwatch();
  134. return false;
  135. }
  136. /**
  137. * Compare and delete
  138. *
  139. * @param string $key
  140. * @param mixed $old
  141. * @return bool
  142. */
  143. public function cad($key, $old) {
  144. self::$cache->watch($this->getNameSpace() . $key);
  145. if ($this->get($key) === $old) {
  146. $result = self::$cache->multi()
  147. ->del($this->getNameSpace() . $key)
  148. ->exec();
  149. return ($result === false) ? false : true;
  150. }
  151. self::$cache->unwatch();
  152. return false;
  153. }
  154. public function setTTL($key, $ttl) {
  155. self::$cache->expire($this->getNameSpace() . $key, $ttl);
  156. }
  157. static public function isAvailable() {
  158. return \OC::$server->getGetRedisFactory()->isAvailable();
  159. }
  160. }