1
0

Factory.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 OCP\Cache\CappedMemoryCache;
  9. use OCP\ICache;
  10. use OCP\ICacheFactory;
  11. use OCP\IMemcache;
  12. use OCP\Profiler\IProfiler;
  13. use Psr\Log\LoggerInterface;
  14. class Factory implements ICacheFactory {
  15. public const NULL_CACHE = NullCache::class;
  16. private string $globalPrefix;
  17. private LoggerInterface $logger;
  18. /**
  19. * @var ?class-string<ICache> $localCacheClass
  20. */
  21. private ?string $localCacheClass;
  22. /**
  23. * @var ?class-string<ICache> $distributedCacheClass
  24. */
  25. private ?string $distributedCacheClass;
  26. /**
  27. * @var ?class-string<IMemcache> $lockingCacheClass
  28. */
  29. private ?string $lockingCacheClass;
  30. private string $logFile;
  31. private IProfiler $profiler;
  32. /**
  33. * @param string $globalPrefix
  34. * @param LoggerInterface $logger
  35. * @param ?class-string<ICache> $localCacheClass
  36. * @param ?class-string<ICache> $distributedCacheClass
  37. * @param ?class-string<IMemcache> $lockingCacheClass
  38. * @param string $logFile
  39. */
  40. public function __construct(string $globalPrefix, LoggerInterface $logger, IProfiler $profiler,
  41. ?string $localCacheClass = null, ?string $distributedCacheClass = null, ?string $lockingCacheClass = null, string $logFile = '') {
  42. $this->logFile = $logFile;
  43. $this->globalPrefix = $globalPrefix;
  44. if (!$localCacheClass) {
  45. $localCacheClass = self::NULL_CACHE;
  46. }
  47. $localCacheClass = ltrim($localCacheClass, '\\');
  48. if (!$distributedCacheClass) {
  49. $distributedCacheClass = $localCacheClass;
  50. }
  51. $distributedCacheClass = ltrim($distributedCacheClass, '\\');
  52. $missingCacheMessage = 'Memcache {class} not available for {use} cache';
  53. $missingCacheHint = 'Is the matching PHP module installed and enabled?';
  54. if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) {
  55. throw new \OCP\HintException(strtr($missingCacheMessage, [
  56. '{class}' => $localCacheClass, '{use}' => 'local'
  57. ]), $missingCacheHint);
  58. }
  59. if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) {
  60. throw new \OCP\HintException(strtr($missingCacheMessage, [
  61. '{class}' => $distributedCacheClass, '{use}' => 'distributed'
  62. ]), $missingCacheHint);
  63. }
  64. if (!($lockingCacheClass && class_exists($lockingCacheClass) && $lockingCacheClass::isAvailable())) {
  65. // don't fall back since the fallback might not be suitable for storing lock
  66. $lockingCacheClass = self::NULL_CACHE;
  67. }
  68. $lockingCacheClass = ltrim($lockingCacheClass, '\\');
  69. $this->localCacheClass = $localCacheClass;
  70. $this->distributedCacheClass = $distributedCacheClass;
  71. $this->lockingCacheClass = $lockingCacheClass;
  72. $this->profiler = $profiler;
  73. }
  74. /**
  75. * create a cache instance for storing locks
  76. *
  77. * @param string $prefix
  78. * @return IMemcache
  79. */
  80. public function createLocking(string $prefix = ''): IMemcache {
  81. assert($this->lockingCacheClass !== null);
  82. $cache = new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix);
  83. if ($this->lockingCacheClass === Redis::class && $this->profiler->isEnabled()) {
  84. // We only support the profiler with Redis
  85. $cache = new ProfilerWrapperCache($cache, 'Locking');
  86. $this->profiler->add($cache);
  87. }
  88. if ($this->lockingCacheClass === Redis::class &&
  89. $this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  90. $cache = new LoggerWrapperCache($cache, $this->logFile);
  91. }
  92. return $cache;
  93. }
  94. /**
  95. * create a distributed cache instance
  96. *
  97. * @param string $prefix
  98. * @return ICache
  99. */
  100. public function createDistributed(string $prefix = ''): ICache {
  101. assert($this->distributedCacheClass !== null);
  102. $cache = new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix);
  103. if ($this->distributedCacheClass === Redis::class && $this->profiler->isEnabled()) {
  104. // We only support the profiler with Redis
  105. $cache = new ProfilerWrapperCache($cache, 'Distributed');
  106. $this->profiler->add($cache);
  107. }
  108. if ($this->distributedCacheClass === Redis::class && $this->logFile !== ''
  109. && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  110. $cache = new LoggerWrapperCache($cache, $this->logFile);
  111. }
  112. return $cache;
  113. }
  114. /**
  115. * create a local cache instance
  116. *
  117. * @param string $prefix
  118. * @return ICache
  119. */
  120. public function createLocal(string $prefix = ''): ICache {
  121. assert($this->localCacheClass !== null);
  122. $cache = new $this->localCacheClass($this->globalPrefix . '/' . $prefix);
  123. if ($this->localCacheClass === Redis::class && $this->profiler->isEnabled()) {
  124. // We only support the profiler with Redis
  125. $cache = new ProfilerWrapperCache($cache, 'Local');
  126. $this->profiler->add($cache);
  127. }
  128. if ($this->localCacheClass === Redis::class && $this->logFile !== ''
  129. && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  130. $cache = new LoggerWrapperCache($cache, $this->logFile);
  131. }
  132. return $cache;
  133. }
  134. /**
  135. * check memcache availability
  136. *
  137. * @return bool
  138. */
  139. public function isAvailable(): bool {
  140. return $this->distributedCacheClass !== self::NULL_CACHE;
  141. }
  142. public function createInMemory(int $capacity = 512): ICache {
  143. return new CappedMemoryCache($capacity);
  144. }
  145. /**
  146. * Check if a local memory cache backend is available
  147. *
  148. * @return bool
  149. */
  150. public function isLocalCacheAvailable(): bool {
  151. return $this->localCacheClass !== self::NULL_CACHE;
  152. }
  153. }