Factory.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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->logger = $logger;
  67. $this->logFile = $logFile;
  68. $this->globalPrefix = $globalPrefix;
  69. if (!$localCacheClass) {
  70. $localCacheClass = self::NULL_CACHE;
  71. }
  72. if (!$distributedCacheClass) {
  73. $distributedCacheClass = $localCacheClass;
  74. }
  75. $missingCacheMessage = 'Memcache {class} not available for {use} cache';
  76. $missingCacheHint = 'Is the matching PHP module installed and enabled?';
  77. if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) {
  78. throw new \OCP\HintException(strtr($missingCacheMessage, [
  79. '{class}' => $localCacheClass, '{use}' => 'local'
  80. ]), $missingCacheHint);
  81. }
  82. if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) {
  83. throw new \OCP\HintException(strtr($missingCacheMessage, [
  84. '{class}' => $distributedCacheClass, '{use}' => 'distributed'
  85. ]), $missingCacheHint);
  86. }
  87. if (!($lockingCacheClass && class_exists($lockingCacheClass) && $lockingCacheClass::isAvailable())) {
  88. // don't fallback since the fallback might not be suitable for storing lock
  89. $lockingCacheClass = self::NULL_CACHE;
  90. }
  91. $this->localCacheClass = $localCacheClass;
  92. $this->distributedCacheClass = $distributedCacheClass;
  93. $this->lockingCacheClass = $lockingCacheClass;
  94. $this->profiler = $profiler;
  95. }
  96. /**
  97. * create a cache instance for storing locks
  98. *
  99. * @param string $prefix
  100. * @return IMemcache
  101. */
  102. public function createLocking(string $prefix = ''): IMemcache {
  103. assert($this->lockingCacheClass !== null);
  104. $cache = new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix);
  105. if ($this->profiler->isEnabled() && $this->lockingCacheClass === '\OC\Memcache\Redis') {
  106. // We only support the profiler with Redis
  107. $cache = new ProfilerWrapperCache($cache, 'Locking');
  108. $this->profiler->add($cache);
  109. }
  110. if ($this->lockingCacheClass === Redis::class &&
  111. $this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  112. $cache = new LoggerWrapperCache($cache, $this->logFile);
  113. }
  114. return $cache;
  115. }
  116. /**
  117. * create a distributed cache instance
  118. *
  119. * @param string $prefix
  120. * @return ICache
  121. */
  122. public function createDistributed(string $prefix = ''): ICache {
  123. assert($this->distributedCacheClass !== null);
  124. $cache = new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix);
  125. if ($this->profiler->isEnabled() && $this->distributedCacheClass === '\OC\Memcache\Redis') {
  126. // We only support the profiler with Redis
  127. $cache = new ProfilerWrapperCache($cache, 'Distributed');
  128. $this->profiler->add($cache);
  129. }
  130. if ($this->distributedCacheClass === Redis::class && $this->logFile !== ''
  131. && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  132. $cache = new LoggerWrapperCache($cache, $this->logFile);
  133. }
  134. return $cache;
  135. }
  136. /**
  137. * create a local cache instance
  138. *
  139. * @param string $prefix
  140. * @return ICache
  141. */
  142. public function createLocal(string $prefix = ''): ICache {
  143. assert($this->localCacheClass !== null);
  144. $cache = new $this->localCacheClass($this->globalPrefix . '/' . $prefix);
  145. if ($this->profiler->isEnabled() && $this->localCacheClass === '\OC\Memcache\Redis') {
  146. // We only support the profiler with Redis
  147. $cache = new ProfilerWrapperCache($cache, 'Local');
  148. $this->profiler->add($cache);
  149. }
  150. if ($this->localCacheClass === Redis::class && $this->logFile !== ''
  151. && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  152. $cache = new LoggerWrapperCache($cache, $this->logFile);
  153. }
  154. return $cache;
  155. }
  156. /**
  157. * check memcache availability
  158. *
  159. * @return bool
  160. */
  161. public function isAvailable(): bool {
  162. return ($this->distributedCacheClass !== self::NULL_CACHE);
  163. }
  164. /**
  165. * @see \OC\Memcache\Factory::createLocal()
  166. * @param string $prefix
  167. * @return ICache
  168. */
  169. public function createLowLatency(string $prefix = ''): ICache {
  170. return $this->createLocal($prefix);
  171. }
  172. /**
  173. * Check if a local memory cache backend is available
  174. *
  175. * @return bool
  176. */
  177. public function isLocalCacheAvailable(): bool {
  178. return ($this->localCacheClass !== self::NULL_CACHE);
  179. }
  180. }