1
0

Factory.php 6.0 KB

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