1
0

APCu.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Memcache;
  8. use bantu\IniGetWrapper\IniGetWrapper;
  9. use OCP\IMemcache;
  10. class APCu extends Cache implements IMemcache {
  11. use CASTrait {
  12. cas as casEmulated;
  13. }
  14. use CADTrait;
  15. public function get($key) {
  16. $result = apcu_fetch($this->getPrefix() . $key, $success);
  17. if (!$success) {
  18. return null;
  19. }
  20. return $result;
  21. }
  22. public function set($key, $value, $ttl = 0) {
  23. if ($ttl === 0) {
  24. $ttl = self::DEFAULT_TTL;
  25. }
  26. return apcu_store($this->getPrefix() . $key, $value, $ttl);
  27. }
  28. public function hasKey($key) {
  29. return apcu_exists($this->getPrefix() . $key);
  30. }
  31. public function remove($key) {
  32. return apcu_delete($this->getPrefix() . $key);
  33. }
  34. public function clear($prefix = '') {
  35. $ns = $this->getPrefix() . $prefix;
  36. $ns = preg_quote($ns, '/');
  37. if (class_exists('\APCIterator')) {
  38. $iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY);
  39. } else {
  40. $iter = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY);
  41. }
  42. return apcu_delete($iter);
  43. }
  44. /**
  45. * Set a value in the cache if it's not already stored
  46. *
  47. * @param string $key
  48. * @param mixed $value
  49. * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
  50. * @return bool
  51. */
  52. public function add($key, $value, $ttl = 0) {
  53. if ($ttl === 0) {
  54. $ttl = self::DEFAULT_TTL;
  55. }
  56. return apcu_add($this->getPrefix() . $key, $value, $ttl);
  57. }
  58. /**
  59. * Increase a stored number
  60. *
  61. * @param string $key
  62. * @param int $step
  63. * @return int | bool
  64. */
  65. public function inc($key, $step = 1) {
  66. $success = null;
  67. return apcu_inc($this->getPrefix() . $key, $step, $success, self::DEFAULT_TTL);
  68. }
  69. /**
  70. * Decrease a stored number
  71. *
  72. * @param string $key
  73. * @param int $step
  74. * @return int | bool
  75. */
  76. public function dec($key, $step = 1) {
  77. return apcu_exists($this->getPrefix() . $key)
  78. ? apcu_dec($this->getPrefix() . $key, $step)
  79. : false;
  80. }
  81. /**
  82. * Compare and set
  83. *
  84. * @param string $key
  85. * @param mixed $old
  86. * @param mixed $new
  87. * @return bool
  88. */
  89. public function cas($key, $old, $new) {
  90. // apc only does cas for ints
  91. if (is_int($old) and is_int($new)) {
  92. return apcu_cas($this->getPrefix() . $key, $old, $new);
  93. } else {
  94. return $this->casEmulated($key, $old, $new);
  95. }
  96. }
  97. public static function isAvailable(): bool {
  98. if (!extension_loaded('apcu')) {
  99. return false;
  100. } elseif (!\OC::$server->get(IniGetWrapper::class)->getBool('apc.enabled')) {
  101. return false;
  102. } elseif (!\OC::$server->get(IniGetWrapper::class)->getBool('apc.enable_cli') && \OC::$CLI) {
  103. return false;
  104. } elseif (version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1) {
  105. return false;
  106. } else {
  107. return true;
  108. }
  109. }
  110. }