@@ -37,128 +37,128 @@ |
||
| 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 | - throw new \OC\HintException(strtr($missingCacheMessage, [ |
|
| 90 | - '{class}' => $localCacheClass, '{use}' => 'local' |
|
| 91 | - ]), $missingCacheHint); |
|
| 92 | - } |
|
| 93 | - if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) { |
|
| 94 | - throw new \OC\HintException(strtr($missingCacheMessage, [ |
|
| 95 | - '{class}' => $distributedCacheClass, '{use}' => 'distributed' |
|
| 96 | - ]), $missingCacheHint); |
|
| 97 | - } |
|
| 98 | - if (!($lockingCacheClass && class_exists($distributedCacheClass) && $lockingCacheClass::isAvailable())) { |
|
| 99 | - // don't fallback since the fallback might not be suitable for storing lock |
|
| 100 | - $lockingCacheClass = self::NULL_CACHE; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $this->localCacheClass = $localCacheClass; |
|
| 104 | - $this->distributedCacheClass = $distributedCacheClass; |
|
| 105 | - $this->lockingCacheClass = $lockingCacheClass; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * create a cache instance for storing locks |
|
| 110 | - * |
|
| 111 | - * @param string $prefix |
|
| 112 | - * @return IMemcache |
|
| 113 | - */ |
|
| 114 | - public function createLocking(string $prefix = ''): IMemcache { |
|
| 115 | - return new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * create a distributed cache instance |
|
| 120 | - * |
|
| 121 | - * @param string $prefix |
|
| 122 | - * @return ICache |
|
| 123 | - */ |
|
| 124 | - public function createDistributed(string $prefix = ''): ICache { |
|
| 125 | - return new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * create a local cache instance |
|
| 130 | - * |
|
| 131 | - * @param string $prefix |
|
| 132 | - * @return ICache |
|
| 133 | - */ |
|
| 134 | - public function createLocal(string $prefix = ''): ICache { |
|
| 135 | - return new $this->localCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * check memcache availability |
|
| 140 | - * |
|
| 141 | - * @return bool |
|
| 142 | - */ |
|
| 143 | - public function isAvailable(): bool { |
|
| 144 | - return ($this->distributedCacheClass !== self::NULL_CACHE); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @see \OC\Memcache\Factory::createLocal() |
|
| 149 | - * @param string $prefix |
|
| 150 | - * @return ICache |
|
| 151 | - */ |
|
| 152 | - public function createLowLatency(string $prefix = ''): ICache { |
|
| 153 | - return $this->createLocal($prefix); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Check if a local memory cache backend is available |
|
| 158 | - * |
|
| 159 | - * @return bool |
|
| 160 | - */ |
|
| 161 | - public function isLocalCacheAvailable(): bool { |
|
| 162 | - return ($this->localCacheClass !== self::NULL_CACHE); |
|
| 163 | - } |
|
| 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 | + throw new \OC\HintException(strtr($missingCacheMessage, [ |
|
| 90 | + '{class}' => $localCacheClass, '{use}' => 'local' |
|
| 91 | + ]), $missingCacheHint); |
|
| 92 | + } |
|
| 93 | + if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) { |
|
| 94 | + throw new \OC\HintException(strtr($missingCacheMessage, [ |
|
| 95 | + '{class}' => $distributedCacheClass, '{use}' => 'distributed' |
|
| 96 | + ]), $missingCacheHint); |
|
| 97 | + } |
|
| 98 | + if (!($lockingCacheClass && class_exists($distributedCacheClass) && $lockingCacheClass::isAvailable())) { |
|
| 99 | + // don't fallback since the fallback might not be suitable for storing lock |
|
| 100 | + $lockingCacheClass = self::NULL_CACHE; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $this->localCacheClass = $localCacheClass; |
|
| 104 | + $this->distributedCacheClass = $distributedCacheClass; |
|
| 105 | + $this->lockingCacheClass = $lockingCacheClass; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * create a cache instance for storing locks |
|
| 110 | + * |
|
| 111 | + * @param string $prefix |
|
| 112 | + * @return IMemcache |
|
| 113 | + */ |
|
| 114 | + public function createLocking(string $prefix = ''): IMemcache { |
|
| 115 | + return new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * create a distributed cache instance |
|
| 120 | + * |
|
| 121 | + * @param string $prefix |
|
| 122 | + * @return ICache |
|
| 123 | + */ |
|
| 124 | + public function createDistributed(string $prefix = ''): ICache { |
|
| 125 | + return new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * create a local cache instance |
|
| 130 | + * |
|
| 131 | + * @param string $prefix |
|
| 132 | + * @return ICache |
|
| 133 | + */ |
|
| 134 | + public function createLocal(string $prefix = ''): ICache { |
|
| 135 | + return new $this->localCacheClass($this->globalPrefix . '/' . $prefix); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * check memcache availability |
|
| 140 | + * |
|
| 141 | + * @return bool |
|
| 142 | + */ |
|
| 143 | + public function isAvailable(): bool { |
|
| 144 | + return ($this->distributedCacheClass !== self::NULL_CACHE); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @see \OC\Memcache\Factory::createLocal() |
|
| 149 | + * @param string $prefix |
|
| 150 | + * @return ICache |
|
| 151 | + */ |
|
| 152 | + public function createLowLatency(string $prefix = ''): ICache { |
|
| 153 | + return $this->createLocal($prefix); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Check if a local memory cache backend is available |
|
| 158 | + * |
|
| 159 | + * @return bool |
|
| 160 | + */ |
|
| 161 | + public function isLocalCacheAvailable(): bool { |
|
| 162 | + return ($this->localCacheClass !== self::NULL_CACHE); |
|
| 163 | + } |
|
| 164 | 164 | } |