xcache.php 3.4 KB

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