XCache.php 3.6 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 Roeland Jago Douma <roeland@famdouma.nl>
  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 OCP\IMemcache;
  31. /**
  32. * See http://xcache.lighttpd.net/wiki/XcacheApi for provided constants and
  33. * functions etc.
  34. */
  35. class XCache extends Cache implements IMemcache {
  36. use CASTrait;
  37. use CADTrait;
  38. /**
  39. * entries in XCache gets namespaced to prevent collisions between ownCloud instances and users
  40. */
  41. protected function getNameSpace() {
  42. return $this->prefix;
  43. }
  44. public function get($key) {
  45. return xcache_get($this->getNameSpace() . $key);
  46. }
  47. public function set($key, $value, $ttl = 0) {
  48. if ($ttl > 0) {
  49. return xcache_set($this->getNameSpace() . $key, $value, $ttl);
  50. } else {
  51. return xcache_set($this->getNameSpace() . $key, $value);
  52. }
  53. }
  54. public function hasKey($key) {
  55. return xcache_isset($this->getNameSpace() . $key);
  56. }
  57. public function remove($key) {
  58. return xcache_unset($this->getNameSpace() . $key);
  59. }
  60. public function clear($prefix = '') {
  61. if (function_exists('xcache_unset_by_prefix')) {
  62. return xcache_unset_by_prefix($this->getNameSpace() . $prefix);
  63. } else {
  64. // Since we can not clear by prefix, we just clear the whole cache.
  65. xcache_clear_cache(\XC_TYPE_VAR, 0);
  66. }
  67. return true;
  68. }
  69. /**
  70. * Set a value in the cache if it's not already stored
  71. *
  72. * @param string $key
  73. * @param mixed $value
  74. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  75. * @return bool
  76. */
  77. public function add($key, $value, $ttl = 0) {
  78. if ($this->hasKey($key)) {
  79. return false;
  80. } else {
  81. return $this->set($key, $value, $ttl);
  82. }
  83. }
  84. /**
  85. * Increase a stored number
  86. *
  87. * @param string $key
  88. * @param int $step
  89. * @return int | bool
  90. */
  91. public function inc($key, $step = 1) {
  92. return xcache_inc($this->getPrefix() . $key, $step);
  93. }
  94. /**
  95. * Decrease a stored number
  96. *
  97. * @param string $key
  98. * @param int $step
  99. * @return int | bool
  100. */
  101. public function dec($key, $step = 1) {
  102. return xcache_dec($this->getPrefix() . $key, $step);
  103. }
  104. static public function isAvailable() {
  105. if (!extension_loaded('xcache')) {
  106. return false;
  107. }
  108. if (\OC::$CLI && !getenv('XCACHE_TEST')) {
  109. return false;
  110. }
  111. if (!function_exists('xcache_unset_by_prefix') && \OC::$server->getIniWrapper()->getBool('xcache.admin.enable_auth')) {
  112. // We do not want to use XCache if we can not clear it without
  113. // using the administration function xcache_clear_cache()
  114. // AND administration functions are password-protected.
  115. return false;
  116. }
  117. $var_size = \OC::$server->getIniWrapper()->getBytes('xcache.var_size');
  118. if (!$var_size) {
  119. return false;
  120. }
  121. return true;
  122. }
  123. }