Redis.php 4.7 KB

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