Factory.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  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 Robin Appelman <robin@icewind.nl>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Stefan Weil <sw@weilnetz.de>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\Memcache;
  32. use OCP\ICache;
  33. use OCP\ICacheFactory;
  34. use OCP\ILogger;
  35. use OCP\IMemcache;
  36. class Factory implements ICacheFactory {
  37. const NULL_CACHE = NullCache::class;
  38. /**
  39. * @var string $globalPrefix
  40. */
  41. private $globalPrefix;
  42. /**
  43. * @var ILogger $logger
  44. */
  45. private $logger;
  46. /**
  47. * @var string $localCacheClass
  48. */
  49. private $localCacheClass;
  50. /**
  51. * @var string $distributedCacheClass
  52. */
  53. private $distributedCacheClass;
  54. /**
  55. * @var string $lockingCacheClass
  56. */
  57. private $lockingCacheClass;
  58. /**
  59. * @param string $globalPrefix
  60. * @param ILogger $logger
  61. * @param string|null $localCacheClass
  62. * @param string|null $distributedCacheClass
  63. * @param string|null $lockingCacheClass
  64. */
  65. public function __construct(string $globalPrefix, ILogger $logger,
  66. $localCacheClass = null, $distributedCacheClass = null, $lockingCacheClass = null)
  67. {
  68. $this->logger = $logger;
  69. $this->globalPrefix = $globalPrefix;
  70. if (!$localCacheClass) {
  71. $localCacheClass = self::NULL_CACHE;
  72. }
  73. if (!$distributedCacheClass) {
  74. $distributedCacheClass = $localCacheClass;
  75. }
  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. if (\OC::$CLI && !defined('PHPUNIT_RUN')) {
  80. // CLI should not hard-fail on broken memcache
  81. $this->logger->info($missingCacheMessage, [
  82. 'class' => $localCacheClass,
  83. 'use' => 'local',
  84. 'app' => 'cli'
  85. ]);
  86. $localCacheClass = self::NULL_CACHE;
  87. } else {
  88. throw new \OC\HintException(strtr($missingCacheMessage, [
  89. '{class}' => $localCacheClass, '{use}' => 'local'
  90. ]), $missingCacheHint);
  91. }
  92. }
  93. if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) {
  94. if (\OC::$CLI && !defined('PHPUNIT_RUN')) {
  95. // CLI should not hard-fail on broken memcache
  96. $this->logger->info($missingCacheMessage, [
  97. 'class' => $distributedCacheClass,
  98. 'use' => 'distributed',
  99. 'app' => 'cli'
  100. ]);
  101. $distributedCacheClass = self::NULL_CACHE;
  102. } else {
  103. throw new \OC\HintException(strtr($missingCacheMessage, [
  104. '{class}' => $distributedCacheClass, '{use}' => 'distributed'
  105. ]), $missingCacheHint);
  106. }
  107. }
  108. if (!($lockingCacheClass && class_exists($distributedCacheClass) && $lockingCacheClass::isAvailable())) {
  109. // don't fallback since the fallback might not be suitable for storing lock
  110. $lockingCacheClass = self::NULL_CACHE;
  111. }
  112. $this->localCacheClass = $localCacheClass;
  113. $this->distributedCacheClass = $distributedCacheClass;
  114. $this->lockingCacheClass = $lockingCacheClass;
  115. }
  116. /**
  117. * create a cache instance for storing locks
  118. *
  119. * @param string $prefix
  120. * @return IMemcache
  121. */
  122. public function createLocking(string $prefix = ''): IMemcache {
  123. return new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix);
  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. return new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix);
  133. }
  134. /**
  135. * create a local cache instance
  136. *
  137. * @param string $prefix
  138. * @return ICache
  139. */
  140. public function createLocal(string $prefix = ''): ICache {
  141. return new $this->localCacheClass($this->globalPrefix . '/' . $prefix);
  142. }
  143. /**
  144. * @see \OC\Memcache\Factory::createDistributed()
  145. * @param string $prefix
  146. * @return ICache
  147. * @deprecated 13.0.0 Use either createLocking, createDistributed or createLocal
  148. */
  149. public function create(string $prefix = ''): ICache {
  150. return $this->createDistributed($prefix);
  151. }
  152. /**
  153. * check memcache availability
  154. *
  155. * @return bool
  156. */
  157. public function isAvailable(): bool {
  158. return ($this->distributedCacheClass !== self::NULL_CACHE);
  159. }
  160. /**
  161. * @see \OC\Memcache\Factory::createLocal()
  162. * @param string $prefix
  163. * @return ICache
  164. */
  165. public function createLowLatency(string $prefix = ''): ICache {
  166. return $this->createLocal($prefix);
  167. }
  168. /**
  169. * Check if a local memory cache backend is available
  170. *
  171. * @return bool
  172. */
  173. public function isLocalCacheAvailable(): bool {
  174. return ($this->localCacheClass !== self::NULL_CACHE);
  175. }
  176. }