MemcacheConfigured.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OC\Memcache\Memcached;
  9. use OCP\ICacheFactory;
  10. use OCP\IConfig;
  11. use OCP\IL10N;
  12. use OCP\IURLGenerator;
  13. use OCP\SetupCheck\ISetupCheck;
  14. use OCP\SetupCheck\SetupResult;
  15. class MemcacheConfigured implements ISetupCheck {
  16. public function __construct(
  17. private IL10N $l10n,
  18. private IConfig $config,
  19. private IURLGenerator $urlGenerator,
  20. private ICacheFactory $cacheFactory,
  21. ) {
  22. }
  23. public function getName(): string {
  24. return $this->l10n->t('Memcache');
  25. }
  26. public function getCategory(): string {
  27. return 'system';
  28. }
  29. public function run(): SetupResult {
  30. $memcacheDistributedClass = $this->config->getSystemValue('memcache.distributed', null);
  31. $memcacheLockingClass = $this->config->getSystemValue('memcache.locking', null);
  32. $memcacheLocalClass = $this->config->getSystemValue('memcache.local', null);
  33. $caches = array_filter([$memcacheDistributedClass,$memcacheLockingClass,$memcacheLocalClass]);
  34. if (in_array(Memcached::class, array_map(fn (string $class) => ltrim($class, '\\'), $caches))) {
  35. // wrong PHP module is installed
  36. if (extension_loaded('memcache') && !extension_loaded('memcached')) {
  37. return SetupResult::warning(
  38. $this->l10n->t('Memcached is configured as distributed cache, but the wrong PHP module ("memcache") is installed. Please install the PHP module "memcached".')
  39. );
  40. }
  41. // required PHP module is missing
  42. if (!extension_loaded('memcached')) {
  43. return SetupResult::warning(
  44. $this->l10n->t('Memcached is configured as distributed cache, but the PHP module "memcached" is not installed. Please install the PHP module "memcached".')
  45. );
  46. }
  47. }
  48. if ($memcacheLocalClass === null) {
  49. return SetupResult::info(
  50. $this->l10n->t('No memory cache has been configured. To enhance performance, please configure a memcache, if available.'),
  51. $this->urlGenerator->linkToDocs('admin-performance')
  52. );
  53. }
  54. if ($this->cacheFactory->isLocalCacheAvailable()) {
  55. $random = random_bytes(64);
  56. $local = $this->cacheFactory->createLocal('setupcheck.local');
  57. try {
  58. $local->set('test', $random);
  59. $local2 = $this->cacheFactory->createLocal('setupcheck.local');
  60. $actual = $local2->get('test');
  61. $local->remove('test');
  62. } catch (\Throwable) {
  63. $actual = null;
  64. }
  65. if ($actual !== $random) {
  66. return SetupResult::error($this->l10n->t('Failed to write and read a value from local cache.'));
  67. }
  68. }
  69. if ($this->cacheFactory->isAvailable()) {
  70. $random = random_bytes(64);
  71. $distributed = $this->cacheFactory->createDistributed('setupcheck');
  72. try {
  73. $distributed->set('test', $random);
  74. $distributed2 = $this->cacheFactory->createDistributed('setupcheck');
  75. $actual = $distributed2->get('test');
  76. $distributed->remove('test');
  77. } catch (\Throwable) {
  78. $actual = null;
  79. }
  80. if ($actual !== $random) {
  81. return SetupResult::error($this->l10n->t('Failed to write and read a value from distributed cache.'));
  82. }
  83. }
  84. return SetupResult::success($this->l10n->t('Configured'));
  85. }
  86. }