Redis.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 $cache
  35. */
  36. private static $cache = null;
  37. public function __construct($prefix = '', string $logFile = '') {
  38. parent::__construct($prefix);
  39. if (is_null(self::$cache)) {
  40. self::$cache = \OC::$server->getGetRedisFactory()->getInstance();
  41. }
  42. }
  43. public function get($key) {
  44. $result = self::$cache->get($this->getPrefix() . $key);
  45. if ($result === false && !self::$cache->exists($this->getPrefix() . $key)) {
  46. return null;
  47. } else {
  48. return json_decode($result, true);
  49. }
  50. }
  51. public function set($key, $value, $ttl = 0) {
  52. if ($ttl > 0) {
  53. return self::$cache->setex($this->getPrefix() . $key, $ttl, json_encode($value));
  54. } else {
  55. return self::$cache->set($this->getPrefix() . $key, json_encode($value));
  56. }
  57. }
  58. public function hasKey($key) {
  59. return (bool)self::$cache->exists($this->getPrefix() . $key);
  60. }
  61. public function remove($key) {
  62. if (self::$cache->del($this->getPrefix() . $key)) {
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. }
  68. public function clear($prefix = '') {
  69. $prefix = $this->getPrefix() . $prefix . '*';
  70. $keys = self::$cache->keys($prefix);
  71. $deleted = self::$cache->del($keys);
  72. return count($keys) === $deleted;
  73. }
  74. /**
  75. * Set a value in the cache if it's not already stored
  76. *
  77. * @param string $key
  78. * @param mixed $value
  79. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  80. * @return bool
  81. */
  82. public function add($key, $value, $ttl = 0) {
  83. // don't encode ints for inc/dec
  84. if (!is_int($value)) {
  85. $value = json_encode($value);
  86. }
  87. $args = ['nx'];
  88. if ($ttl !== 0 && is_int($ttl)) {
  89. $args['ex'] = $ttl;
  90. }
  91. return self::$cache->set($this->getPrefix() . $key, $value, $args);
  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->getPrefix() . $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->getPrefix() . $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->getPrefix() . $key);
  129. if ($this->get($key) === $old) {
  130. $result = self::$cache->multi()
  131. ->set($this->getPrefix() . $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->getPrefix() . $key);
  147. if ($this->get($key) === $old) {
  148. $result = self::$cache->multi()
  149. ->del($this->getPrefix() . $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->getPrefix() . $key, $ttl);
  158. }
  159. public static function isAvailable(): bool {
  160. return \OC::$server->getGetRedisFactory()->isAvailable();
  161. }
  162. }