You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Redis.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. private $logFile;
  38. public function __construct($prefix = '', string $logFile = '') {
  39. parent::__construct($prefix);
  40. $this->logFile = $logFile;
  41. if (is_null(self::$cache)) {
  42. self::$cache = \OC::$server->getGetRedisFactory()->getInstance();
  43. }
  44. }
  45. /**
  46. * entries in redis get namespaced to prevent collisions between ownCloud instances and users
  47. */
  48. protected function getNameSpace() {
  49. return $this->prefix;
  50. }
  51. public function get($key) {
  52. if ($this->logFile !== '' && is_writable($this->logFile)) {
  53. file_put_contents(
  54. $this->logFile,
  55. $this->getNameSpace() . '::get::' . $key . "\n",
  56. FILE_APPEND
  57. );
  58. }
  59. $result = self::$cache->get($this->getNameSpace() . $key);
  60. if ($result === false && !self::$cache->exists($this->getNameSpace() . $key)) {
  61. return null;
  62. } else {
  63. return json_decode($result, true);
  64. }
  65. }
  66. public function set($key, $value, $ttl = 0) {
  67. if ($this->logFile !== '' && is_writable($this->logFile)) {
  68. file_put_contents(
  69. $this->logFile,
  70. $this->getNameSpace() . '::set::' . $key . '::' . $ttl . '::' . json_encode($value) . "\n",
  71. FILE_APPEND
  72. );
  73. }
  74. if ($ttl > 0) {
  75. return self::$cache->setex($this->getNameSpace() . $key, $ttl, json_encode($value));
  76. } else {
  77. return self::$cache->set($this->getNameSpace() . $key, json_encode($value));
  78. }
  79. }
  80. public function hasKey($key) {
  81. if ($this->logFile !== '' && is_writable($this->logFile)) {
  82. file_put_contents(
  83. $this->logFile,
  84. $this->getNameSpace() . '::hasKey::' . $key . "\n",
  85. FILE_APPEND
  86. );
  87. }
  88. return (bool)self::$cache->exists($this->getNameSpace() . $key);
  89. }
  90. public function remove($key) {
  91. if ($this->logFile !== '' && is_writable($this->logFile)) {
  92. file_put_contents(
  93. $this->logFile,
  94. $this->getNameSpace() . '::remove::' . $key . "\n",
  95. FILE_APPEND
  96. );
  97. }
  98. if (self::$cache->del($this->getNameSpace() . $key)) {
  99. return true;
  100. } else {
  101. return false;
  102. }
  103. }
  104. public function clear($prefix = '') {
  105. if ($this->logFile !== '' && is_writable($this->logFile)) {
  106. file_put_contents(
  107. $this->logFile,
  108. $this->getNameSpace() . '::clear::' . $prefix . "\n",
  109. FILE_APPEND
  110. );
  111. }
  112. $prefix = $this->getNameSpace() . $prefix . '*';
  113. $keys = self::$cache->keys($prefix);
  114. $deleted = self::$cache->del($keys);
  115. return count($keys) === $deleted;
  116. }
  117. /**
  118. * Set a value in the cache if it's not already stored
  119. *
  120. * @param string $key
  121. * @param mixed $value
  122. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  123. * @return bool
  124. */
  125. public function add($key, $value, $ttl = 0) {
  126. // don't encode ints for inc/dec
  127. if (!is_int($value)) {
  128. $value = json_encode($value);
  129. }
  130. $args = ['nx'];
  131. if ($ttl !== 0 && is_int($ttl)) {
  132. $args['ex'] = $ttl;
  133. }
  134. if ($this->logFile !== '' && is_writable($this->logFile)) {
  135. file_put_contents(
  136. $this->logFile,
  137. $this->getNameSpace() . '::add::' . $key . '::' . $value . "\n",
  138. FILE_APPEND
  139. );
  140. }
  141. return self::$cache->set($this->getPrefix() . $key, $value, $args);
  142. }
  143. /**
  144. * Increase a stored number
  145. *
  146. * @param string $key
  147. * @param int $step
  148. * @return int | bool
  149. */
  150. public function inc($key, $step = 1) {
  151. if ($this->logFile !== '' && is_writable($this->logFile)) {
  152. file_put_contents(
  153. $this->logFile,
  154. $this->getNameSpace() . '::inc::' . $key . "\n",
  155. FILE_APPEND
  156. );
  157. }
  158. return self::$cache->incrBy($this->getNameSpace() . $key, $step);
  159. }
  160. /**
  161. * Decrease a stored number
  162. *
  163. * @param string $key
  164. * @param int $step
  165. * @return int | bool
  166. */
  167. public function dec($key, $step = 1) {
  168. if ($this->logFile !== '' && is_writable($this->logFile)) {
  169. file_put_contents(
  170. $this->logFile,
  171. $this->getNameSpace() . '::dec::' . $key . "\n",
  172. FILE_APPEND
  173. );
  174. }
  175. if (!$this->hasKey($key)) {
  176. return false;
  177. }
  178. return self::$cache->decrBy($this->getNameSpace() . $key, $step);
  179. }
  180. /**
  181. * Compare and set
  182. *
  183. * @param string $key
  184. * @param mixed $old
  185. * @param mixed $new
  186. * @return bool
  187. */
  188. public function cas($key, $old, $new) {
  189. if ($this->logFile !== '' && is_writable($this->logFile)) {
  190. file_put_contents(
  191. $this->logFile,
  192. $this->getNameSpace() . '::cas::' . $key . "\n",
  193. FILE_APPEND
  194. );
  195. }
  196. if (!is_int($new)) {
  197. $new = json_encode($new);
  198. }
  199. self::$cache->watch($this->getNameSpace() . $key);
  200. if ($this->get($key) === $old) {
  201. $result = self::$cache->multi()
  202. ->set($this->getNameSpace() . $key, $new)
  203. ->exec();
  204. return $result !== false;
  205. }
  206. self::$cache->unwatch();
  207. return false;
  208. }
  209. /**
  210. * Compare and delete
  211. *
  212. * @param string $key
  213. * @param mixed $old
  214. * @return bool
  215. */
  216. public function cad($key, $old) {
  217. if ($this->logFile !== '' && is_writable($this->logFile)) {
  218. file_put_contents(
  219. $this->logFile,
  220. $this->getNameSpace() . '::cad::' . $key . "\n",
  221. FILE_APPEND
  222. );
  223. }
  224. self::$cache->watch($this->getNameSpace() . $key);
  225. if ($this->get($key) === $old) {
  226. $result = self::$cache->multi()
  227. ->del($this->getNameSpace() . $key)
  228. ->exec();
  229. return $result !== false;
  230. }
  231. self::$cache->unwatch();
  232. return false;
  233. }
  234. public function setTTL($key, $ttl) {
  235. self::$cache->expire($this->getNameSpace() . $key, $ttl);
  236. }
  237. public static function isAvailable() {
  238. return \OC::$server->getGetRedisFactory()->isAvailable();
  239. }
  240. }