1
0

PhpApcuConfig.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OC\Memcache\APCu;
  9. use OCP\IConfig;
  10. use OCP\IL10N;
  11. use OCP\SetupCheck\ISetupCheck;
  12. use OCP\SetupCheck\SetupResult;
  13. class PhpApcuConfig implements ISetupCheck {
  14. public const USAGE_RATE_WARNING = 90;
  15. public const AGE_WARNING = 3600 * 8;
  16. public function __construct(
  17. private IL10N $l10n,
  18. private IConfig $config,
  19. ) {
  20. }
  21. public function getCategory(): string {
  22. return 'php';
  23. }
  24. public function getName(): string {
  25. return $this->l10n->t('PHP APCu configuration');
  26. }
  27. public function run(): SetupResult {
  28. $localIsApcu = ltrim($this->config->getSystemValueString('memcache.local'), '\\') === APCu::class;
  29. $distributedIsApcu = ltrim($this->config->getSystemValueString('memcache.distributed'), '\\') === APCu::class;
  30. if (!$localIsApcu && !$distributedIsApcu) {
  31. return SetupResult::success();
  32. }
  33. if (!APCu::isAvailable()) {
  34. return SetupResult::success();
  35. }
  36. $cache = apcu_cache_info(true);
  37. $mem = apcu_sma_info(true);
  38. if ($cache === false || $mem === false) {
  39. return SetupResult::success();
  40. }
  41. $expunges = $cache['expunges'];
  42. $memSize = $mem['num_seg'] * $mem['seg_size'];
  43. $memAvailable = $mem['avail_mem'];
  44. $memUsed = $memSize - $memAvailable;
  45. $usageRate = round($memUsed / $memSize * 100, 0);
  46. $elapsed = max(time() - $cache['start_time'], 1);
  47. if ($expunges > 0 && $elapsed < self::AGE_WARNING) {
  48. return SetupResult::warning($this->l10n->t('Your APCu cache has been running full, consider increasing the apc.shm_size php setting.'));
  49. }
  50. if ($usageRate > self::USAGE_RATE_WARNING) {
  51. return SetupResult::warning($this->l10n->t('Your APCu cache is almost full at %s%%, consider increasing the apc.shm_size php setting.', [$usageRate]));
  52. }
  53. return SetupResult::success();
  54. }
  55. }