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 4.7KB

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