Memcached.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  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 Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Memcache;
  30. use OC\HintException;
  31. use OCP\IMemcache;
  32. class Memcached extends Cache implements IMemcache {
  33. use CASTrait;
  34. /**
  35. * @var \Memcached $cache
  36. */
  37. private static $cache = null;
  38. use CADTrait;
  39. public function __construct($prefix = '') {
  40. parent::__construct($prefix);
  41. if (is_null(self::$cache)) {
  42. self::$cache = new \Memcached();
  43. $defaultOptions = [
  44. \Memcached::OPT_CONNECT_TIMEOUT => 50,
  45. \Memcached::OPT_RETRY_TIMEOUT => 50,
  46. \Memcached::OPT_SEND_TIMEOUT => 50,
  47. \Memcached::OPT_RECV_TIMEOUT => 50,
  48. \Memcached::OPT_POLL_TIMEOUT => 50,
  49. // Enable compression
  50. \Memcached::OPT_COMPRESSION => true,
  51. // Turn on consistent hashing
  52. \Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
  53. // Enable Binary Protocol
  54. //\Memcached::OPT_BINARY_PROTOCOL => true,
  55. ];
  56. // by default enable igbinary serializer if available
  57. if (\Memcached::HAVE_IGBINARY) {
  58. $defaultOptions[\Memcached::OPT_SERIALIZER] =
  59. \Memcached::SERIALIZER_IGBINARY;
  60. }
  61. $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []);
  62. if (is_array($options)) {
  63. $options = $options + $defaultOptions;
  64. self::$cache->setOptions($options);
  65. } else {
  66. throw new HintException("Expected 'memcached_options' config to be an array, got $options");
  67. }
  68. $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers');
  69. if (!$servers) {
  70. $server = \OC::$server->getSystemConfig()->getValue('memcached_server');
  71. if ($server) {
  72. $servers = [$server];
  73. } else {
  74. $servers = [['localhost', 11211]];
  75. }
  76. }
  77. self::$cache->addServers($servers);
  78. }
  79. }
  80. /**
  81. * entries in XCache gets namespaced to prevent collisions between owncloud instances and users
  82. */
  83. protected function getNameSpace() {
  84. return $this->prefix;
  85. }
  86. public function get($key) {
  87. $result = self::$cache->get($this->getNamespace() . $key);
  88. if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
  89. return null;
  90. } else {
  91. return $result;
  92. }
  93. }
  94. public function set($key, $value, $ttl = 0) {
  95. if ($ttl > 0) {
  96. $result = self::$cache->set($this->getNamespace() . $key, $value, $ttl);
  97. } else {
  98. $result = self::$cache->set($this->getNamespace() . $key, $value);
  99. }
  100. if ($result !== true) {
  101. $this->verifyReturnCode();
  102. }
  103. return $result;
  104. }
  105. public function hasKey($key) {
  106. self::$cache->get($this->getNamespace() . $key);
  107. return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
  108. }
  109. public function remove($key) {
  110. $result= self::$cache->delete($this->getNamespace() . $key);
  111. if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
  112. $this->verifyReturnCode();
  113. }
  114. return $result;
  115. }
  116. public function clear($prefix = '') {
  117. $prefix = $this->getNamespace() . $prefix;
  118. $allKeys = self::$cache->getAllKeys();
  119. if ($allKeys === false) {
  120. // newer Memcached doesn't like getAllKeys(), flush everything
  121. self::$cache->flush();
  122. return true;
  123. }
  124. $keys = array();
  125. $prefixLength = strlen($prefix);
  126. foreach ($allKeys as $key) {
  127. if (substr($key, 0, $prefixLength) === $prefix) {
  128. $keys[] = $key;
  129. }
  130. }
  131. if (method_exists(self::$cache, 'deleteMulti')) {
  132. self::$cache->deleteMulti($keys);
  133. } else {
  134. foreach ($keys as $key) {
  135. self::$cache->delete($key);
  136. }
  137. }
  138. return true;
  139. }
  140. /**
  141. * Set a value in the cache if it's not already stored
  142. *
  143. * @param string $key
  144. * @param mixed $value
  145. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  146. * @return bool
  147. * @throws \Exception
  148. */
  149. public function add($key, $value, $ttl = 0) {
  150. $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
  151. if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
  152. $this->verifyReturnCode();
  153. }
  154. return $result;
  155. }
  156. /**
  157. * Increase a stored number
  158. *
  159. * @param string $key
  160. * @param int $step
  161. * @return int | bool
  162. */
  163. public function inc($key, $step = 1) {
  164. $this->add($key, 0);
  165. $result = self::$cache->increment($this->getPrefix() . $key, $step);
  166. if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
  167. return false;
  168. }
  169. return $result;
  170. }
  171. /**
  172. * Decrease a stored number
  173. *
  174. * @param string $key
  175. * @param int $step
  176. * @return int | bool
  177. */
  178. public function dec($key, $step = 1) {
  179. $result = self::$cache->decrement($this->getPrefix() . $key, $step);
  180. if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
  181. return false;
  182. }
  183. return $result;
  184. }
  185. static public function isAvailable() {
  186. return extension_loaded('memcached');
  187. }
  188. /**
  189. * @throws \Exception
  190. */
  191. private function verifyReturnCode() {
  192. $code = self::$cache->getResultCode();
  193. if ($code === \Memcached::RES_SUCCESS) {
  194. return;
  195. }
  196. $message = self::$cache->getResultMessage();
  197. throw new \Exception("Error $code interacting with memcached : $message");
  198. }
  199. }