Factory.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 = null;
  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 callable $globalPrefixClosure
  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(
  41. private $globalPrefixClosure,
  42. LoggerInterface $logger,
  43. IProfiler $profiler,
  44. ?string $localCacheClass = null,
  45. ?string $distributedCacheClass = null,
  46. ?string $lockingCacheClass = null,
  47. string $logFile = ''
  48. ) {
  49. $this->logFile = $logFile;
  50. if (!$localCacheClass) {
  51. $localCacheClass = self::NULL_CACHE;
  52. }
  53. $localCacheClass = ltrim($localCacheClass, '\\');
  54. if (!$distributedCacheClass) {
  55. $distributedCacheClass = $localCacheClass;
  56. }
  57. $distributedCacheClass = ltrim($distributedCacheClass, '\\');
  58. $missingCacheMessage = 'Memcache {class} not available for {use} cache';
  59. $missingCacheHint = 'Is the matching PHP module installed and enabled?';
  60. if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) {
  61. throw new \OCP\HintException(strtr($missingCacheMessage, [
  62. '{class}' => $localCacheClass, '{use}' => 'local'
  63. ]), $missingCacheHint);
  64. }
  65. if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) {
  66. throw new \OCP\HintException(strtr($missingCacheMessage, [
  67. '{class}' => $distributedCacheClass, '{use}' => 'distributed'
  68. ]), $missingCacheHint);
  69. }
  70. if (!($lockingCacheClass && class_exists($lockingCacheClass) && $lockingCacheClass::isAvailable())) {
  71. // don't fall back since the fallback might not be suitable for storing lock
  72. $lockingCacheClass = self::NULL_CACHE;
  73. }
  74. $lockingCacheClass = ltrim($lockingCacheClass, '\\');
  75. $this->localCacheClass = $localCacheClass;
  76. $this->distributedCacheClass = $distributedCacheClass;
  77. $this->lockingCacheClass = $lockingCacheClass;
  78. $this->profiler = $profiler;
  79. }
  80. private function getGlobalPrefix(): ?string {
  81. if (is_null($this->globalPrefix)) {
  82. $this->globalPrefix = ($this->globalPrefixClosure)();
  83. }
  84. return $this->globalPrefix;
  85. }
  86. /**
  87. * create a cache instance for storing locks
  88. *
  89. * @param string $prefix
  90. * @return IMemcache
  91. */
  92. public function createLocking(string $prefix = ''): IMemcache {
  93. $globalPrefix = $this->getGlobalPrefix();
  94. if (is_null($globalPrefix)) {
  95. return new ArrayCache($prefix);
  96. }
  97. assert($this->lockingCacheClass !== null);
  98. $cache = new $this->lockingCacheClass($globalPrefix . '/' . $prefix);
  99. if ($this->lockingCacheClass === Redis::class && $this->profiler->isEnabled()) {
  100. // We only support the profiler with Redis
  101. $cache = new ProfilerWrapperCache($cache, 'Locking');
  102. $this->profiler->add($cache);
  103. }
  104. if ($this->lockingCacheClass === Redis::class &&
  105. $this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  106. $cache = new LoggerWrapperCache($cache, $this->logFile);
  107. }
  108. return $cache;
  109. }
  110. /**
  111. * create a distributed cache instance
  112. *
  113. * @param string $prefix
  114. * @return ICache
  115. */
  116. public function createDistributed(string $prefix = ''): ICache {
  117. $globalPrefix = $this->getGlobalPrefix();
  118. if (is_null($globalPrefix)) {
  119. return new ArrayCache($prefix);
  120. }
  121. assert($this->distributedCacheClass !== null);
  122. $cache = new $this->distributedCacheClass($globalPrefix . '/' . $prefix);
  123. if ($this->distributedCacheClass === Redis::class && $this->profiler->isEnabled()) {
  124. // We only support the profiler with Redis
  125. $cache = new ProfilerWrapperCache($cache, 'Distributed');
  126. $this->profiler->add($cache);
  127. }
  128. if ($this->distributedCacheClass === 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. * create a local cache instance
  136. *
  137. * @param string $prefix
  138. * @return ICache
  139. */
  140. public function createLocal(string $prefix = ''): ICache {
  141. $globalPrefix = $this->getGlobalPrefix();
  142. if (is_null($globalPrefix)) {
  143. return new ArrayCache($prefix);
  144. }
  145. assert($this->localCacheClass !== null);
  146. $cache = new $this->localCacheClass($globalPrefix . '/' . $prefix);
  147. if ($this->localCacheClass === Redis::class && $this->profiler->isEnabled()) {
  148. // We only support the profiler with Redis
  149. $cache = new ProfilerWrapperCache($cache, 'Local');
  150. $this->profiler->add($cache);
  151. }
  152. if ($this->localCacheClass === Redis::class && $this->logFile !== ''
  153. && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  154. $cache = new LoggerWrapperCache($cache, $this->logFile);
  155. }
  156. return $cache;
  157. }
  158. /**
  159. * check memcache availability
  160. *
  161. * @return bool
  162. */
  163. public function isAvailable(): bool {
  164. return $this->distributedCacheClass !== self::NULL_CACHE;
  165. }
  166. public function createInMemory(int $capacity = 512): ICache {
  167. return new CappedMemoryCache($capacity);
  168. }
  169. /**
  170. * Check if a local memory cache backend is available
  171. *
  172. * @return bool
  173. */
  174. public function isLocalCacheAvailable(): bool {
  175. return $this->localCacheClass !== self::NULL_CACHE;
  176. }
  177. }