Factory.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Markus Goetz <markus@woboq.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Richard Steinmetz <richard@steinmetz.cloud>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Robin McCorkell <robin@mccorkell.me.uk>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Stefan Weil <sw@weilnetz.de>
  15. * @author Vincent Petry <vincent@nextcloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\Memcache;
  33. use OCP\Profiler\IProfiler;
  34. use OCP\ICache;
  35. use OCP\ICacheFactory;
  36. use OCP\IMemcache;
  37. use Psr\Log\LoggerInterface;
  38. class Factory implements ICacheFactory {
  39. public const NULL_CACHE = NullCache::class;
  40. private string $globalPrefix;
  41. private LoggerInterface $logger;
  42. /**
  43. * @var ?class-string<ICache> $localCacheClass
  44. */
  45. private ?string $localCacheClass;
  46. /**
  47. * @var ?class-string<ICache> $distributedCacheClass
  48. */
  49. private ?string $distributedCacheClass;
  50. /**
  51. * @var ?class-string<IMemcache> $lockingCacheClass
  52. */
  53. private ?string $lockingCacheClass;
  54. private string $logFile;
  55. private IProfiler $profiler;
  56. /**
  57. * @param string $globalPrefix
  58. * @param LoggerInterface $logger
  59. * @param ?class-string<ICache> $localCacheClass
  60. * @param ?class-string<ICache> $distributedCacheClass
  61. * @param ?class-string<IMemcache> $lockingCacheClass
  62. * @param string $logFile
  63. */
  64. public function __construct(string $globalPrefix, LoggerInterface $logger, IProfiler $profiler,
  65. ?string $localCacheClass = null, ?string $distributedCacheClass = null, ?string $lockingCacheClass = null, string $logFile = '') {
  66. $this->logFile = $logFile;
  67. $this->globalPrefix = $globalPrefix;
  68. if (!$localCacheClass) {
  69. $localCacheClass = self::NULL_CACHE;
  70. }
  71. $localCacheClass = ltrim($localCacheClass, '\\');
  72. if (!$distributedCacheClass) {
  73. $distributedCacheClass = $localCacheClass;
  74. }
  75. $distributedCacheClass = ltrim($distributedCacheClass, '\\');
  76. $missingCacheMessage = 'Memcache {class} not available for {use} cache';
  77. $missingCacheHint = 'Is the matching PHP module installed and enabled?';
  78. if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) {
  79. throw new \OCP\HintException(strtr($missingCacheMessage, [
  80. '{class}' => $localCacheClass, '{use}' => 'local'
  81. ]), $missingCacheHint);
  82. }
  83. if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) {
  84. throw new \OCP\HintException(strtr($missingCacheMessage, [
  85. '{class}' => $distributedCacheClass, '{use}' => 'distributed'
  86. ]), $missingCacheHint);
  87. }
  88. if (!($lockingCacheClass && class_exists($lockingCacheClass) && $lockingCacheClass::isAvailable())) {
  89. // don't fall back since the fallback might not be suitable for storing lock
  90. $lockingCacheClass = self::NULL_CACHE;
  91. }
  92. $lockingCacheClass = ltrim($lockingCacheClass, '\\');
  93. $this->localCacheClass = $localCacheClass;
  94. $this->distributedCacheClass = $distributedCacheClass;
  95. $this->lockingCacheClass = $lockingCacheClass;
  96. $this->profiler = $profiler;
  97. }
  98. /**
  99. * create a cache instance for storing locks
  100. *
  101. * @param string $prefix
  102. * @return IMemcache
  103. */
  104. public function createLocking(string $prefix = ''): IMemcache {
  105. assert($this->lockingCacheClass !== null);
  106. $cache = new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix);
  107. if ($this->lockingCacheClass === Redis::class && $this->profiler->isEnabled()) {
  108. // We only support the profiler with Redis
  109. $cache = new ProfilerWrapperCache($cache, 'Locking');
  110. $this->profiler->add($cache);
  111. }
  112. if ($this->lockingCacheClass === Redis::class &&
  113. $this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  114. $cache = new LoggerWrapperCache($cache, $this->logFile);
  115. }
  116. return $cache;
  117. }
  118. /**
  119. * create a distributed cache instance
  120. *
  121. * @param string $prefix
  122. * @return ICache
  123. */
  124. public function createDistributed(string $prefix = ''): ICache {
  125. assert($this->distributedCacheClass !== null);
  126. $cache = new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix);
  127. if ($this->distributedCacheClass === Redis::class && $this->profiler->isEnabled()) {
  128. // We only support the profiler with Redis
  129. $cache = new ProfilerWrapperCache($cache, 'Distributed');
  130. $this->profiler->add($cache);
  131. }
  132. if ($this->distributedCacheClass === Redis::class && $this->logFile !== ''
  133. && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  134. $cache = new LoggerWrapperCache($cache, $this->logFile);
  135. }
  136. return $cache;
  137. }
  138. /**
  139. * create a local cache instance
  140. *
  141. * @param string $prefix
  142. * @return ICache
  143. */
  144. public function createLocal(string $prefix = ''): ICache {
  145. assert($this->localCacheClass !== null);
  146. $cache = new $this->localCacheClass($this->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. /**
  167. * @see \OC\Memcache\Factory::createLocal()
  168. * @param string $prefix
  169. * @return ICache
  170. */
  171. public function createLowLatency(string $prefix = ''): ICache {
  172. return $this->createLocal($prefix);
  173. }
  174. /**
  175. * Check if a local memory cache backend is available
  176. *
  177. * @return bool
  178. */
  179. public function isLocalCacheAvailable(): bool {
  180. return $this->localCacheClass !== self::NULL_CACHE;
  181. }
  182. }