Memcached.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers');
  44. if (!$servers) {
  45. $server = \OC::$server->getSystemConfig()->getValue('memcached_server');
  46. if ($server) {
  47. $servers = array($server);
  48. } else {
  49. $servers = array(array('localhost', 11211));
  50. }
  51. }
  52. self::$cache->addServers($servers);
  53. $defaultOptions = [
  54. \Memcached::OPT_CONNECT_TIMEOUT => 50,
  55. \Memcached::OPT_RETRY_TIMEOUT => 50,
  56. \Memcached::OPT_SEND_TIMEOUT => 50,
  57. \Memcached::OPT_RECV_TIMEOUT => 50,
  58. \Memcached::OPT_POLL_TIMEOUT => 50,
  59. // Enable compression
  60. \Memcached::OPT_COMPRESSION => true,
  61. // Turn on consistent hashing
  62. \Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
  63. // Enable Binary Protocol
  64. \Memcached::OPT_BINARY_PROTOCOL => true,
  65. ];
  66. // by default enable igbinary serializer if available
  67. if (\Memcached::HAVE_IGBINARY) {
  68. $defaultOptions[\Memcached::OPT_SERIALIZER] =
  69. \Memcached::SERIALIZER_IGBINARY;
  70. }
  71. $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []);
  72. if (is_array($options)) {
  73. $options = $options + $defaultOptions;
  74. self::$cache->setOptions($options);
  75. } else {
  76. throw new HintException("Expected 'memcached_options' config to be an array, got $options");
  77. }
  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. $this->verifyReturnCode();
  101. return $result;
  102. }
  103. public function hasKey($key) {
  104. self::$cache->get($this->getNamespace() . $key);
  105. return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
  106. }
  107. public function remove($key) {
  108. $result= self::$cache->delete($this->getNamespace() . $key);
  109. if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
  110. $this->verifyReturnCode();
  111. }
  112. return $result;
  113. }
  114. public function clear($prefix = '') {
  115. $prefix = $this->getNamespace() . $prefix;
  116. $allKeys = self::$cache->getAllKeys();
  117. if ($allKeys === false) {
  118. // newer Memcached doesn't like getAllKeys(), flush everything
  119. self::$cache->flush();
  120. return true;
  121. }
  122. $keys = array();
  123. $prefixLength = strlen($prefix);
  124. foreach ($allKeys as $key) {
  125. if (substr($key, 0, $prefixLength) === $prefix) {
  126. $keys[] = $key;
  127. }
  128. }
  129. if (method_exists(self::$cache, 'deleteMulti')) {
  130. self::$cache->deleteMulti($keys);
  131. } else {
  132. foreach ($keys as $key) {
  133. self::$cache->delete($key);
  134. }
  135. }
  136. return true;
  137. }
  138. /**
  139. * Set a value in the cache if it's not already stored
  140. *
  141. * @param string $key
  142. * @param mixed $value
  143. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  144. * @return bool
  145. * @throws \Exception
  146. */
  147. public function add($key, $value, $ttl = 0) {
  148. $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
  149. if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
  150. $this->verifyReturnCode();
  151. }
  152. return $result;
  153. }
  154. /**
  155. * Increase a stored number
  156. *
  157. * @param string $key
  158. * @param int $step
  159. * @return int | bool
  160. */
  161. public function inc($key, $step = 1) {
  162. $this->add($key, 0);
  163. $result = self::$cache->increment($this->getPrefix() . $key, $step);
  164. if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
  165. return false;
  166. }
  167. return $result;
  168. }
  169. /**
  170. * Decrease a stored number
  171. *
  172. * @param string $key
  173. * @param int $step
  174. * @return int | bool
  175. */
  176. public function dec($key, $step = 1) {
  177. $result = self::$cache->decrement($this->getPrefix() . $key, $step);
  178. if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
  179. return false;
  180. }
  181. return $result;
  182. }
  183. static public function isAvailable() {
  184. return extension_loaded('memcached');
  185. }
  186. /**
  187. * @throws \Exception
  188. */
  189. private function verifyReturnCode() {
  190. $code = self::$cache->getResultCode();
  191. if ($code === \Memcached::RES_SUCCESS) {
  192. return;
  193. }
  194. $message = self::$cache->getResultMessage();
  195. throw new \Exception("Error $code interacting with memcached : $message");
  196. }
  197. }