apc.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Otto Sabart <ottosabart@seberm.com>
  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 APC extends Cache implements IMemcache {
  27. use CASTrait {
  28. cas as casEmulated;
  29. }
  30. public function get($key) {
  31. $result = apc_fetch($this->getPrefix() . $key, $success);
  32. if (!$success) {
  33. return null;
  34. }
  35. return $result;
  36. }
  37. public function set($key, $value, $ttl = 0) {
  38. return apc_store($this->getPrefix() . $key, $value, $ttl);
  39. }
  40. public function hasKey($key) {
  41. return apc_exists($this->getPrefix() . $key);
  42. }
  43. public function remove($key) {
  44. return apc_delete($this->getPrefix() . $key);
  45. }
  46. public function clear($prefix = '') {
  47. $ns = $this->getPrefix() . $prefix;
  48. $ns = preg_quote($ns, '/');
  49. $iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY);
  50. return apc_delete($iter);
  51. }
  52. /**
  53. * Set a value in the cache if it's not already stored
  54. *
  55. * @param string $key
  56. * @param mixed $value
  57. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  58. * @return bool
  59. */
  60. public function add($key, $value, $ttl = 0) {
  61. return apc_add($this->getPrefix() . $key, $value, $ttl);
  62. }
  63. /**
  64. * Increase a stored number
  65. *
  66. * @param string $key
  67. * @param int $step
  68. * @return int | bool
  69. */
  70. public function inc($key, $step = 1) {
  71. $this->add($key, 0);
  72. return apc_inc($this->getPrefix() . $key, $step);
  73. }
  74. /**
  75. * Decrease a stored number
  76. *
  77. * @param string $key
  78. * @param int $step
  79. * @return int | bool
  80. */
  81. public function dec($key, $step = 1) {
  82. return apc_dec($this->getPrefix() . $key, $step);
  83. }
  84. /**
  85. * Compare and set
  86. *
  87. * @param string $key
  88. * @param mixed $old
  89. * @param mixed $new
  90. * @return bool
  91. */
  92. public function cas($key, $old, $new) {
  93. // apc only does cas for ints
  94. if (is_int($old) and is_int($new)) {
  95. return apc_cas($this->getPrefix() . $key, $old, $new);
  96. } else {
  97. return $this->casEmulated($key, $old, $new);
  98. }
  99. }
  100. static public function isAvailable() {
  101. if (!extension_loaded('apc')) {
  102. return false;
  103. } elseif (!ini_get('apc.enabled')) {
  104. return false;
  105. } elseif (!ini_get('apc.enable_cli') && \OC::$CLI) {
  106. return false;
  107. } else {
  108. return true;
  109. }
  110. }
  111. }
  112. if (!function_exists('apc_exists')) {
  113. function apc_exists($keys) {
  114. $result = false;
  115. apc_fetch($keys, $result);
  116. return $result;
  117. }
  118. }