Factory.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. if (\OC::$CLI && !defined('PHPUNIT_RUN') && $localCacheClass === APCu::class) {
  63. // CLI should not fail if APCu is not available but fallback to NullCache.
  64. // This can be the case if APCu is used without apc.enable_cli=1.
  65. // APCu however cannot be shared between PHP instances (CLI and web) anyway.
  66. $localCacheClass = self::NULL_CACHE;
  67. } else {
  68. throw new \OCP\HintException(strtr($missingCacheMessage, [
  69. '{class}' => $localCacheClass, '{use}' => 'local'
  70. ]), $missingCacheHint);
  71. }
  72. }
  73. if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) {
  74. if (\OC::$CLI && !defined('PHPUNIT_RUN') && $distributedCacheClass === APCu::class) {
  75. // CLI should not fail if APCu is not available but fallback to NullCache.
  76. // This can be the case if APCu is used without apc.enable_cli=1.
  77. // APCu however cannot be shared between Nextcloud (PHP) instances anyway.
  78. $distributedCacheClass = self::NULL_CACHE;
  79. } else {
  80. throw new \OCP\HintException(strtr($missingCacheMessage, [
  81. '{class}' => $distributedCacheClass, '{use}' => 'distributed'
  82. ]), $missingCacheHint);
  83. }
  84. }
  85. if (!($lockingCacheClass && class_exists($lockingCacheClass) && $lockingCacheClass::isAvailable())) {
  86. // don't fall back since the fallback might not be suitable for storing lock
  87. $lockingCacheClass = self::NULL_CACHE;
  88. }
  89. $lockingCacheClass = ltrim($lockingCacheClass, '\\');
  90. $this->localCacheClass = $localCacheClass;
  91. $this->distributedCacheClass = $distributedCacheClass;
  92. $this->lockingCacheClass = $lockingCacheClass;
  93. $this->profiler = $profiler;
  94. }
  95. private function getGlobalPrefix(): ?string {
  96. if (is_null($this->globalPrefix)) {
  97. $this->globalPrefix = ($this->globalPrefixClosure)();
  98. }
  99. return $this->globalPrefix;
  100. }
  101. /**
  102. * create a cache instance for storing locks
  103. *
  104. * @param string $prefix
  105. * @return IMemcache
  106. */
  107. public function createLocking(string $prefix = ''): IMemcache {
  108. $globalPrefix = $this->getGlobalPrefix();
  109. if (is_null($globalPrefix)) {
  110. return new ArrayCache($prefix);
  111. }
  112. assert($this->lockingCacheClass !== null);
  113. $cache = new $this->lockingCacheClass($globalPrefix . '/' . $prefix);
  114. if ($this->lockingCacheClass === Redis::class && $this->profiler->isEnabled()) {
  115. // We only support the profiler with Redis
  116. $cache = new ProfilerWrapperCache($cache, 'Locking');
  117. $this->profiler->add($cache);
  118. }
  119. if ($this->lockingCacheClass === Redis::class &&
  120. $this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  121. $cache = new LoggerWrapperCache($cache, $this->logFile);
  122. }
  123. return $cache;
  124. }
  125. /**
  126. * create a distributed cache instance
  127. *
  128. * @param string $prefix
  129. * @return ICache
  130. */
  131. public function createDistributed(string $prefix = ''): ICache {
  132. $globalPrefix = $this->getGlobalPrefix();
  133. if (is_null($globalPrefix)) {
  134. return new ArrayCache($prefix);
  135. }
  136. assert($this->distributedCacheClass !== null);
  137. $cache = new $this->distributedCacheClass($globalPrefix . '/' . $prefix);
  138. if ($this->distributedCacheClass === Redis::class && $this->profiler->isEnabled()) {
  139. // We only support the profiler with Redis
  140. $cache = new ProfilerWrapperCache($cache, 'Distributed');
  141. $this->profiler->add($cache);
  142. }
  143. if ($this->distributedCacheClass === Redis::class && $this->logFile !== ''
  144. && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  145. $cache = new LoggerWrapperCache($cache, $this->logFile);
  146. }
  147. return $cache;
  148. }
  149. /**
  150. * create a local cache instance
  151. *
  152. * @param string $prefix
  153. * @return ICache
  154. */
  155. public function createLocal(string $prefix = ''): ICache {
  156. $globalPrefix = $this->getGlobalPrefix();
  157. if (is_null($globalPrefix)) {
  158. return new ArrayCache($prefix);
  159. }
  160. assert($this->localCacheClass !== null);
  161. $cache = new $this->localCacheClass($globalPrefix . '/' . $prefix);
  162. if ($this->localCacheClass === Redis::class && $this->profiler->isEnabled()) {
  163. // We only support the profiler with Redis
  164. $cache = new ProfilerWrapperCache($cache, 'Local');
  165. $this->profiler->add($cache);
  166. }
  167. if ($this->localCacheClass === Redis::class && $this->logFile !== ''
  168. && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  169. $cache = new LoggerWrapperCache($cache, $this->logFile);
  170. }
  171. return $cache;
  172. }
  173. /**
  174. * check memcache availability
  175. *
  176. * @return bool
  177. */
  178. public function isAvailable(): bool {
  179. return $this->distributedCacheClass !== self::NULL_CACHE;
  180. }
  181. public function createInMemory(int $capacity = 512): ICache {
  182. return new CappedMemoryCache($capacity);
  183. }
  184. /**
  185. * Check if a local memory cache backend is available
  186. *
  187. * @return bool
  188. */
  189. public function isLocalCacheAvailable(): bool {
  190. return $this->localCacheClass !== self::NULL_CACHE;
  191. }
  192. }