memcached.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Memcache;
  25. use OCP\IMemcache;
  26. class Memcached extends Cache implements IMemcache {
  27. use CASTrait;
  28. /**
  29. * @var \Memcached $cache
  30. */
  31. private static $cache = null;
  32. public function __construct($prefix = '') {
  33. parent::__construct($prefix);
  34. if (is_null(self::$cache)) {
  35. self::$cache = new \Memcached();
  36. $servers = \OC_Config::getValue('memcached_servers');
  37. if (!$servers) {
  38. $server = \OC_Config::getValue('memcached_server');
  39. if ($server) {
  40. $servers = array($server);
  41. } else {
  42. $servers = array(array('localhost', 11211));
  43. }
  44. }
  45. self::$cache->addServers($servers);
  46. }
  47. }
  48. /**
  49. * entries in XCache gets namespaced to prevent collisions between owncloud instances and users
  50. */
  51. protected function getNameSpace() {
  52. return $this->prefix;
  53. }
  54. public function get($key) {
  55. $result = self::$cache->get($this->getNamespace() . $key);
  56. if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
  57. return null;
  58. } else {
  59. return $result;
  60. }
  61. }
  62. public function set($key, $value, $ttl = 0) {
  63. if ($ttl > 0) {
  64. return self::$cache->set($this->getNamespace() . $key, $value, $ttl);
  65. } else {
  66. return self::$cache->set($this->getNamespace() . $key, $value);
  67. }
  68. }
  69. public function hasKey($key) {
  70. self::$cache->get($this->getNamespace() . $key);
  71. return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
  72. }
  73. public function remove($key) {
  74. return self::$cache->delete($this->getNamespace() . $key);
  75. }
  76. public function clear($prefix = '') {
  77. $prefix = $this->getNamespace() . $prefix;
  78. $allKeys = self::$cache->getAllKeys();
  79. $keys = array();
  80. $prefixLength = strlen($prefix);
  81. foreach ($allKeys as $key) {
  82. if (substr($key, 0, $prefixLength) === $prefix) {
  83. $keys[] = $key;
  84. }
  85. }
  86. if (method_exists(self::$cache, 'deleteMulti')) {
  87. self::$cache->deleteMulti($keys);
  88. } else {
  89. foreach ($keys as $key) {
  90. self::$cache->delete($key);
  91. }
  92. }
  93. return true;
  94. }
  95. /**
  96. * Set a value in the cache if it's not already stored
  97. *
  98. * @param string $key
  99. * @param mixed $value
  100. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  101. * @return bool
  102. */
  103. public function add($key, $value, $ttl = 0) {
  104. return self::$cache->add($this->getPrefix() . $key, $value, $ttl);
  105. }
  106. /**
  107. * Increase a stored number
  108. *
  109. * @param string $key
  110. * @param int $step
  111. * @return int | bool
  112. */
  113. public function inc($key, $step = 1) {
  114. $this->add($key, 0);
  115. return self::$cache->increment($this->getPrefix() . $key, $step);
  116. }
  117. /**
  118. * Decrease a stored number
  119. *
  120. * @param string $key
  121. * @param int $step
  122. * @return int | bool
  123. */
  124. public function dec($key, $step = 1) {
  125. return self::$cache->decrement($this->getPrefix() . $key, $step);
  126. }
  127. static public function isAvailable() {
  128. return extension_loaded('memcached');
  129. }
  130. }