@@ -37,148 +37,148 @@ |
||
| 37 | 37 | use OCP\IMemcache; |
| 38 | 38 | |
| 39 | 39 | class Factory implements ICacheFactory { |
| 40 | - public const NULL_CACHE = NullCache::class; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var string $globalPrefix |
|
| 44 | - */ |
|
| 45 | - private $globalPrefix; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var ILogger $logger |
|
| 49 | - */ |
|
| 50 | - private $logger; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var string $localCacheClass |
|
| 54 | - */ |
|
| 55 | - private $localCacheClass; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var string $distributedCacheClass |
|
| 59 | - */ |
|
| 60 | - private $distributedCacheClass; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var string $lockingCacheClass |
|
| 64 | - */ |
|
| 65 | - private $lockingCacheClass; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param string $globalPrefix |
|
| 69 | - * @param ILogger $logger |
|
| 70 | - * @param string|null $localCacheClass |
|
| 71 | - * @param string|null $distributedCacheClass |
|
| 72 | - * @param string|null $lockingCacheClass |
|
| 73 | - */ |
|
| 74 | - public function __construct(string $globalPrefix, ILogger $logger, |
|
| 75 | - $localCacheClass = null, $distributedCacheClass = null, $lockingCacheClass = null) { |
|
| 76 | - $this->logger = $logger; |
|
| 77 | - $this->globalPrefix = $globalPrefix; |
|
| 78 | - |
|
| 79 | - if (!$localCacheClass) { |
|
| 80 | - $localCacheClass = self::NULL_CACHE; |
|
| 81 | - } |
|
| 82 | - if (!$distributedCacheClass) { |
|
| 83 | - $distributedCacheClass = $localCacheClass; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $missingCacheMessage = 'Memcache {class} not available for {use} cache'; |
|
| 87 | - $missingCacheHint = 'Is the matching PHP module installed and enabled?'; |
|
| 88 | - if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) { |
|
| 89 | - if (\OC::$CLI && !defined('PHPUNIT_RUN')) { |
|
| 90 | - // CLI should not hard-fail on broken memcache |
|
| 91 | - $this->logger->info($missingCacheMessage, [ |
|
| 92 | - 'class' => $localCacheClass, |
|
| 93 | - 'use' => 'local', |
|
| 94 | - 'app' => 'cli' |
|
| 95 | - ]); |
|
| 96 | - $localCacheClass = self::NULL_CACHE; |
|
| 97 | - } else { |
|
| 98 | - throw new \OC\HintException(strtr($missingCacheMessage, [ |
|
| 99 | - '{class}' => $localCacheClass, '{use}' => 'local' |
|
| 100 | - ]), $missingCacheHint); |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) { |
|
| 104 | - if (\OC::$CLI && !defined('PHPUNIT_RUN')) { |
|
| 105 | - // CLI should not hard-fail on broken memcache |
|
| 106 | - $this->logger->info($missingCacheMessage, [ |
|
| 107 | - 'class' => $distributedCacheClass, |
|
| 108 | - 'use' => 'distributed', |
|
| 109 | - 'app' => 'cli' |
|
| 110 | - ]); |
|
| 111 | - $distributedCacheClass = self::NULL_CACHE; |
|
| 112 | - } else { |
|
| 113 | - throw new \OC\HintException(strtr($missingCacheMessage, [ |
|
| 114 | - '{class}' => $distributedCacheClass, '{use}' => 'distributed' |
|
| 115 | - ]), $missingCacheHint); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - if (!($lockingCacheClass && class_exists($distributedCacheClass) && $lockingCacheClass::isAvailable())) { |
|
| 119 | - // don't fallback since the fallback might not be suitable for storing lock |
|
| 120 | - $lockingCacheClass = self::NULL_CACHE; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $this->localCacheClass = $localCacheClass; |
|
| 124 | - $this->distributedCacheClass = $distributedCacheClass; |
|
| 125 | - $this->lockingCacheClass = $lockingCacheClass; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * create a cache instance for storing locks |
|
| 130 | - * |
|
| 131 | - * @param string $prefix |
|
| 132 | - * @return IMemcache |
|
| 133 | - */ |
|
| 134 | - public function createLocking(string $prefix = ''): IMemcache { |
|
| 135 | - return new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * create a distributed cache instance |
|
| 140 | - * |
|
| 141 | - * @param string $prefix |
|
| 142 | - * @return ICache |
|
| 143 | - */ |
|
| 144 | - public function createDistributed(string $prefix = ''): ICache { |
|
| 145 | - return new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * create a local cache instance |
|
| 150 | - * |
|
| 151 | - * @param string $prefix |
|
| 152 | - * @return ICache |
|
| 153 | - */ |
|
| 154 | - public function createLocal(string $prefix = ''): ICache { |
|
| 155 | - return new $this->localCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * check memcache availability |
|
| 160 | - * |
|
| 161 | - * @return bool |
|
| 162 | - */ |
|
| 163 | - public function isAvailable(): bool { |
|
| 164 | - return ($this->distributedCacheClass !== self::NULL_CACHE); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @see \OC\Memcache\Factory::createLocal() |
|
| 169 | - * @param string $prefix |
|
| 170 | - * @return ICache |
|
| 171 | - */ |
|
| 172 | - public function createLowLatency(string $prefix = ''): ICache { |
|
| 173 | - return $this->createLocal($prefix); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Check if a local memory cache backend is available |
|
| 178 | - * |
|
| 179 | - * @return bool |
|
| 180 | - */ |
|
| 181 | - public function isLocalCacheAvailable(): bool { |
|
| 182 | - return ($this->localCacheClass !== self::NULL_CACHE); |
|
| 183 | - } |
|
| 40 | + public const NULL_CACHE = NullCache::class; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var string $globalPrefix |
|
| 44 | + */ |
|
| 45 | + private $globalPrefix; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var ILogger $logger |
|
| 49 | + */ |
|
| 50 | + private $logger; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var string $localCacheClass |
|
| 54 | + */ |
|
| 55 | + private $localCacheClass; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var string $distributedCacheClass |
|
| 59 | + */ |
|
| 60 | + private $distributedCacheClass; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var string $lockingCacheClass |
|
| 64 | + */ |
|
| 65 | + private $lockingCacheClass; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param string $globalPrefix |
|
| 69 | + * @param ILogger $logger |
|
| 70 | + * @param string|null $localCacheClass |
|
| 71 | + * @param string|null $distributedCacheClass |
|
| 72 | + * @param string|null $lockingCacheClass |
|
| 73 | + */ |
|
| 74 | + public function __construct(string $globalPrefix, ILogger $logger, |
|
| 75 | + $localCacheClass = null, $distributedCacheClass = null, $lockingCacheClass = null) { |
|
| 76 | + $this->logger = $logger; |
|
| 77 | + $this->globalPrefix = $globalPrefix; |
|
| 78 | + |
|
| 79 | + if (!$localCacheClass) { |
|
| 80 | + $localCacheClass = self::NULL_CACHE; |
|
| 81 | + } |
|
| 82 | + if (!$distributedCacheClass) { |
|
| 83 | + $distributedCacheClass = $localCacheClass; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $missingCacheMessage = 'Memcache {class} not available for {use} cache'; |
|
| 87 | + $missingCacheHint = 'Is the matching PHP module installed and enabled?'; |
|
| 88 | + if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) { |
|
| 89 | + if (\OC::$CLI && !defined('PHPUNIT_RUN')) { |
|
| 90 | + // CLI should not hard-fail on broken memcache |
|
| 91 | + $this->logger->info($missingCacheMessage, [ |
|
| 92 | + 'class' => $localCacheClass, |
|
| 93 | + 'use' => 'local', |
|
| 94 | + 'app' => 'cli' |
|
| 95 | + ]); |
|
| 96 | + $localCacheClass = self::NULL_CACHE; |
|
| 97 | + } else { |
|
| 98 | + throw new \OC\HintException(strtr($missingCacheMessage, [ |
|
| 99 | + '{class}' => $localCacheClass, '{use}' => 'local' |
|
| 100 | + ]), $missingCacheHint); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) { |
|
| 104 | + if (\OC::$CLI && !defined('PHPUNIT_RUN')) { |
|
| 105 | + // CLI should not hard-fail on broken memcache |
|
| 106 | + $this->logger->info($missingCacheMessage, [ |
|
| 107 | + 'class' => $distributedCacheClass, |
|
| 108 | + 'use' => 'distributed', |
|
| 109 | + 'app' => 'cli' |
|
| 110 | + ]); |
|
| 111 | + $distributedCacheClass = self::NULL_CACHE; |
|
| 112 | + } else { |
|
| 113 | + throw new \OC\HintException(strtr($missingCacheMessage, [ |
|
| 114 | + '{class}' => $distributedCacheClass, '{use}' => 'distributed' |
|
| 115 | + ]), $missingCacheHint); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + if (!($lockingCacheClass && class_exists($distributedCacheClass) && $lockingCacheClass::isAvailable())) { |
|
| 119 | + // don't fallback since the fallback might not be suitable for storing lock |
|
| 120 | + $lockingCacheClass = self::NULL_CACHE; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $this->localCacheClass = $localCacheClass; |
|
| 124 | + $this->distributedCacheClass = $distributedCacheClass; |
|
| 125 | + $this->lockingCacheClass = $lockingCacheClass; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * create a cache instance for storing locks |
|
| 130 | + * |
|
| 131 | + * @param string $prefix |
|
| 132 | + * @return IMemcache |
|
| 133 | + */ |
|
| 134 | + public function createLocking(string $prefix = ''): IMemcache { |
|
| 135 | + return new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * create a distributed cache instance |
|
| 140 | + * |
|
| 141 | + * @param string $prefix |
|
| 142 | + * @return ICache |
|
| 143 | + */ |
|
| 144 | + public function createDistributed(string $prefix = ''): ICache { |
|
| 145 | + return new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * create a local cache instance |
|
| 150 | + * |
|
| 151 | + * @param string $prefix |
|
| 152 | + * @return ICache |
|
| 153 | + */ |
|
| 154 | + public function createLocal(string $prefix = ''): ICache { |
|
| 155 | + return new $this->localCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * check memcache availability |
|
| 160 | + * |
|
| 161 | + * @return bool |
|
| 162 | + */ |
|
| 163 | + public function isAvailable(): bool { |
|
| 164 | + return ($this->distributedCacheClass !== self::NULL_CACHE); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @see \OC\Memcache\Factory::createLocal() |
|
| 169 | + * @param string $prefix |
|
| 170 | + * @return ICache |
|
| 171 | + */ |
|
| 172 | + public function createLowLatency(string $prefix = ''): ICache { |
|
| 173 | + return $this->createLocal($prefix); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Check if a local memory cache backend is available |
|
| 178 | + * |
|
| 179 | + * @return bool |
|
| 180 | + */ |
|
| 181 | + public function isLocalCacheAvailable(): bool { |
|
| 182 | + return ($this->localCacheClass !== self::NULL_CACHE); |
|
| 183 | + } |
|
| 184 | 184 | } |
@@ -34,46 +34,46 @@ |
||
| 34 | 34 | * @since 7.0.0 |
| 35 | 35 | */ |
| 36 | 36 | interface ICacheFactory { |
| 37 | - /** |
|
| 38 | - * Check if any memory cache backend is available |
|
| 39 | - * |
|
| 40 | - * @return bool |
|
| 41 | - * @since 7.0.0 |
|
| 42 | - */ |
|
| 43 | - public function isAvailable(): bool; |
|
| 37 | + /** |
|
| 38 | + * Check if any memory cache backend is available |
|
| 39 | + * |
|
| 40 | + * @return bool |
|
| 41 | + * @since 7.0.0 |
|
| 42 | + */ |
|
| 43 | + public function isAvailable(): bool; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Check if a local memory cache backend is available |
|
| 47 | - * |
|
| 48 | - * @return bool |
|
| 49 | - * @since 14.0.0 |
|
| 50 | - */ |
|
| 51 | - public function isLocalCacheAvailable(): bool; |
|
| 45 | + /** |
|
| 46 | + * Check if a local memory cache backend is available |
|
| 47 | + * |
|
| 48 | + * @return bool |
|
| 49 | + * @since 14.0.0 |
|
| 50 | + */ |
|
| 51 | + public function isLocalCacheAvailable(): bool; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * create a cache instance for storing locks |
|
| 55 | - * |
|
| 56 | - * @param string $prefix |
|
| 57 | - * @return IMemcache |
|
| 58 | - * @since 13.0.0 |
|
| 59 | - */ |
|
| 60 | - public function createLocking(string $prefix = ''): IMemcache; |
|
| 53 | + /** |
|
| 54 | + * create a cache instance for storing locks |
|
| 55 | + * |
|
| 56 | + * @param string $prefix |
|
| 57 | + * @return IMemcache |
|
| 58 | + * @since 13.0.0 |
|
| 59 | + */ |
|
| 60 | + public function createLocking(string $prefix = ''): IMemcache; |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * create a distributed cache instance |
|
| 64 | - * |
|
| 65 | - * @param string $prefix |
|
| 66 | - * @return ICache |
|
| 67 | - * @since 13.0.0 |
|
| 68 | - */ |
|
| 69 | - public function createDistributed(string $prefix = ''): ICache; |
|
| 62 | + /** |
|
| 63 | + * create a distributed cache instance |
|
| 64 | + * |
|
| 65 | + * @param string $prefix |
|
| 66 | + * @return ICache |
|
| 67 | + * @since 13.0.0 |
|
| 68 | + */ |
|
| 69 | + public function createDistributed(string $prefix = ''): ICache; |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * create a local cache instance |
|
| 73 | - * |
|
| 74 | - * @param string $prefix |
|
| 75 | - * @return ICache |
|
| 76 | - * @since 13.0.0 |
|
| 77 | - */ |
|
| 78 | - public function createLocal(string $prefix = ''): ICache; |
|
| 71 | + /** |
|
| 72 | + * create a local cache instance |
|
| 73 | + * |
|
| 74 | + * @param string $prefix |
|
| 75 | + * @return ICache |
|
| 76 | + * @since 13.0.0 |
|
| 77 | + */ |
|
| 78 | + public function createLocal(string $prefix = ''): ICache; |
|
| 79 | 79 | } |