XCache.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 Clark Tomlinson <fallen013@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Memcache;
  29. use OCP\IMemcache;
  30. /**
  31. * See http://xcache.lighttpd.net/wiki/XcacheApi for provided constants and
  32. * functions etc.
  33. */
  34. class XCache extends Cache implements IMemcache {
  35. use CASTrait;
  36. use CADTrait;
  37. /**
  38. * entries in XCache gets namespaced to prevent collisions between ownCloud instances and users
  39. */
  40. protected function getNameSpace() {
  41. return $this->prefix;
  42. }
  43. public function get($key) {
  44. return xcache_get($this->getNamespace() . $key);
  45. }
  46. public function set($key, $value, $ttl = 0) {
  47. if ($ttl > 0) {
  48. return xcache_set($this->getNamespace() . $key, $value, $ttl);
  49. } else {
  50. return xcache_set($this->getNamespace() . $key, $value);
  51. }
  52. }
  53. public function hasKey($key) {
  54. return xcache_isset($this->getNamespace() . $key);
  55. }
  56. public function remove($key) {
  57. return xcache_unset($this->getNamespace() . $key);
  58. }
  59. public function clear($prefix = '') {
  60. if (function_exists('xcache_unset_by_prefix')) {
  61. return xcache_unset_by_prefix($this->getNamespace() . $prefix);
  62. } else {
  63. // Since we can not clear by prefix, we just clear the whole cache.
  64. xcache_clear_cache(\XC_TYPE_VAR, 0);
  65. }
  66. return true;
  67. }
  68. /**
  69. * Set a value in the cache if it's not already stored
  70. *
  71. * @param string $key
  72. * @param mixed $value
  73. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  74. * @return bool
  75. */
  76. public function add($key, $value, $ttl = 0) {
  77. if ($this->hasKey($key)) {
  78. return false;
  79. } else {
  80. return $this->set($key, $value, $ttl);
  81. }
  82. }
  83. /**
  84. * Increase a stored number
  85. *
  86. * @param string $key
  87. * @param int $step
  88. * @return int | bool
  89. */
  90. public function inc($key, $step = 1) {
  91. return xcache_inc($this->getPrefix() . $key, $step);
  92. }
  93. /**
  94. * Decrease a stored number
  95. *
  96. * @param string $key
  97. * @param int $step
  98. * @return int | bool
  99. */
  100. public function dec($key, $step = 1) {
  101. return xcache_dec($this->getPrefix() . $key, $step);
  102. }
  103. static public function isAvailable() {
  104. if (!extension_loaded('xcache')) {
  105. return false;
  106. }
  107. if (\OC::$CLI && !getenv('XCACHE_TEST')) {
  108. return false;
  109. }
  110. if (!function_exists('xcache_unset_by_prefix') && \OC::$server->getIniWrapper()->getBool('xcache.admin.enable_auth')) {
  111. // We do not want to use XCache if we can not clear it without
  112. // using the administration function xcache_clear_cache()
  113. // AND administration functions are password-protected.
  114. return false;
  115. }
  116. $var_size = \OC::$server->getIniWrapper()->getBytes('xcache.var_size');
  117. if (!$var_size) {
  118. return false;
  119. }
  120. return true;
  121. }
  122. }