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

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