You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Factory.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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\Cache\CappedMemoryCache;
  34. use OCP\ICache;
  35. use OCP\ICacheFactory;
  36. use OCP\IMemcache;
  37. use OCP\Profiler\IProfiler;
  38. use Psr\Log\LoggerInterface;
  39. class Factory implements ICacheFactory {
  40. public const NULL_CACHE = NullCache::class;
  41. private string $globalPrefix;
  42. private LoggerInterface $logger;
  43. /**
  44. * @var ?class-string<ICache> $localCacheClass
  45. */
  46. private ?string $localCacheClass;
  47. /**
  48. * @var ?class-string<ICache> $distributedCacheClass
  49. */
  50. private ?string $distributedCacheClass;
  51. /**
  52. * @var ?class-string<IMemcache> $lockingCacheClass
  53. */
  54. private ?string $lockingCacheClass;
  55. private string $logFile;
  56. private IProfiler $profiler;
  57. /**
  58. * @param string $globalPrefix
  59. * @param LoggerInterface $logger
  60. * @param ?class-string<ICache> $localCacheClass
  61. * @param ?class-string<ICache> $distributedCacheClass
  62. * @param ?class-string<IMemcache> $lockingCacheClass
  63. * @param string $logFile
  64. */
  65. public function __construct(string $globalPrefix, LoggerInterface $logger, IProfiler $profiler,
  66. ?string $localCacheClass = null, ?string $distributedCacheClass = null, ?string $lockingCacheClass = null, string $logFile = '') {
  67. $this->logFile = $logFile;
  68. $this->globalPrefix = $globalPrefix;
  69. if (!$localCacheClass) {
  70. $localCacheClass = self::NULL_CACHE;
  71. }
  72. $localCacheClass = ltrim($localCacheClass, '\\');
  73. if (!$distributedCacheClass) {
  74. $distributedCacheClass = $localCacheClass;
  75. }
  76. $distributedCacheClass = ltrim($distributedCacheClass, '\\');
  77. $missingCacheMessage = 'Memcache {class} not available for {use} cache';
  78. $missingCacheHint = 'Is the matching PHP module installed and enabled?';
  79. if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) {
  80. throw new \OCP\HintException(strtr($missingCacheMessage, [
  81. '{class}' => $localCacheClass, '{use}' => 'local'
  82. ]), $missingCacheHint);
  83. }
  84. if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) {
  85. throw new \OCP\HintException(strtr($missingCacheMessage, [
  86. '{class}' => $distributedCacheClass, '{use}' => 'distributed'
  87. ]), $missingCacheHint);
  88. }
  89. if (!($lockingCacheClass && class_exists($lockingCacheClass) && $lockingCacheClass::isAvailable())) {
  90. // don't fall back since the fallback might not be suitable for storing lock
  91. $lockingCacheClass = self::NULL_CACHE;
  92. }
  93. $lockingCacheClass = ltrim($lockingCacheClass, '\\');
  94. $this->localCacheClass = $localCacheClass;
  95. $this->distributedCacheClass = $distributedCacheClass;
  96. $this->lockingCacheClass = $lockingCacheClass;
  97. $this->profiler = $profiler;
  98. }
  99. /**
  100. * create a cache instance for storing locks
  101. *
  102. * @param string $prefix
  103. * @return IMemcache
  104. */
  105. public function createLocking(string $prefix = ''): IMemcache {
  106. assert($this->lockingCacheClass !== null);
  107. $cache = new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix);
  108. if ($this->lockingCacheClass === Redis::class && $this->profiler->isEnabled()) {
  109. // We only support the profiler with Redis
  110. $cache = new ProfilerWrapperCache($cache, 'Locking');
  111. $this->profiler->add($cache);
  112. }
  113. if ($this->lockingCacheClass === Redis::class &&
  114. $this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  115. $cache = new LoggerWrapperCache($cache, $this->logFile);
  116. }
  117. return $cache;
  118. }
  119. /**
  120. * create a distributed cache instance
  121. *
  122. * @param string $prefix
  123. * @return ICache
  124. */
  125. public function createDistributed(string $prefix = ''): ICache {
  126. assert($this->distributedCacheClass !== null);
  127. $cache = new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix);
  128. if ($this->distributedCacheClass === Redis::class && $this->profiler->isEnabled()) {
  129. // We only support the profiler with Redis
  130. $cache = new ProfilerWrapperCache($cache, 'Distributed');
  131. $this->profiler->add($cache);
  132. }
  133. if ($this->distributedCacheClass === Redis::class && $this->logFile !== ''
  134. && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
  135. $cache = new LoggerWrapperCache($cache, $this->logFile);
  136. }
  137. return $cache;
  138. }
  139. /**
  140. * create a local cache instance
  141. *
  142. * @param string $prefix
  143. * @return ICache
  144. */
  145. public function createLocal(string $prefix = ''): ICache {
  146. assert($this->localCacheClass !== null);
  147. $cache = new $this->localCacheClass($this->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. }