Passed
Push — master ( 95af96...61f135 )
by Roeland
12:27 queued 11s
created

Factory::createLowLatency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Markus Goetz <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Robin McCorkell <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Stefan Weil <[email protected]>
14
 * @author Vincent Petry <[email protected]>
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
32
namespace OC\Memcache;
33
34
use OCP\ICache;
35
use OCP\ICacheFactory;
36
use OCP\ILogger;
37
use OCP\IMemcache;
38
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
	}
184
}
185