1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Andreas Fischer <[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 Stefan Weil <[email protected]> |
13
|
|
|
* @author Thomas Müller <[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
|
|
|
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
|
|
|
{ |
77
|
|
|
$this->logger = $logger; |
78
|
|
|
$this->globalPrefix = $globalPrefix; |
79
|
|
|
|
80
|
|
|
if (!$localCacheClass) { |
|
|
|
|
81
|
|
|
$localCacheClass = self::NULL_CACHE; |
82
|
|
|
} |
83
|
|
|
if (!$distributedCacheClass) { |
|
|
|
|
84
|
|
|
$distributedCacheClass = $localCacheClass; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$missingCacheMessage = 'Memcache {class} not available for {use} cache'; |
88
|
|
|
$missingCacheHint = 'Is the matching PHP module installed and enabled?'; |
89
|
|
View Code Duplication |
if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) { |
90
|
|
|
if (\OC::$CLI && !defined('PHPUNIT_RUN')) { |
91
|
|
|
// CLI should not hard-fail on broken memcache |
92
|
|
|
$this->logger->info($missingCacheMessage, [ |
93
|
|
|
'class' => $localCacheClass, |
94
|
|
|
'use' => 'local', |
95
|
|
|
'app' => 'cli' |
96
|
|
|
]); |
97
|
|
|
$localCacheClass = self::NULL_CACHE; |
98
|
|
|
} else { |
99
|
|
|
throw new \OC\HintException(strtr($missingCacheMessage, [ |
100
|
|
|
'{class}' => $localCacheClass, '{use}' => 'local' |
101
|
|
|
]), $missingCacheHint); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
View Code Duplication |
if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) { |
105
|
|
|
if (\OC::$CLI && !defined('PHPUNIT_RUN')) { |
106
|
|
|
// CLI should not hard-fail on broken memcache |
107
|
|
|
$this->logger->info($missingCacheMessage, [ |
108
|
|
|
'class' => $distributedCacheClass, |
109
|
|
|
'use' => 'distributed', |
110
|
|
|
'app' => 'cli' |
111
|
|
|
]); |
112
|
|
|
$distributedCacheClass = self::NULL_CACHE; |
113
|
|
|
} else { |
114
|
|
|
throw new \OC\HintException(strtr($missingCacheMessage, [ |
115
|
|
|
'{class}' => $distributedCacheClass, '{use}' => 'distributed' |
116
|
|
|
]), $missingCacheHint); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
if (!($lockingCacheClass && class_exists($distributedCacheClass) && $lockingCacheClass::isAvailable())) { |
|
|
|
|
120
|
|
|
// don't fallback since the fallback might not be suitable for storing lock |
121
|
|
|
$lockingCacheClass = self::NULL_CACHE; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->localCacheClass = $localCacheClass; |
125
|
|
|
$this->distributedCacheClass = $distributedCacheClass; |
126
|
|
|
$this->lockingCacheClass = $lockingCacheClass; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* create a cache instance for storing locks |
131
|
|
|
* |
132
|
|
|
* @param string $prefix |
133
|
|
|
* @return IMemcache |
134
|
|
|
*/ |
135
|
|
|
public function createLocking(string $prefix = ''): IMemcache { |
136
|
|
|
return new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* create a distributed cache instance |
141
|
|
|
* |
142
|
|
|
* @param string $prefix |
143
|
|
|
* @return ICache |
144
|
|
|
*/ |
145
|
|
|
public function createDistributed(string $prefix = ''): ICache { |
146
|
|
|
return new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* create a local cache instance |
151
|
|
|
* |
152
|
|
|
* @param string $prefix |
153
|
|
|
* @return ICache |
154
|
|
|
*/ |
155
|
|
|
public function createLocal(string $prefix = ''): ICache { |
156
|
|
|
return new $this->localCacheClass($this->globalPrefix . '/' . $prefix); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @see \OC\Memcache\Factory::createDistributed() |
161
|
|
|
* @param string $prefix |
162
|
|
|
* @return ICache |
163
|
|
|
* @deprecated 13.0.0 Use either createLocking, createDistributed or createLocal |
164
|
|
|
*/ |
165
|
|
|
public function create(string $prefix = ''): ICache { |
166
|
|
|
return $this->createDistributed($prefix); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* check memcache availability |
171
|
|
|
* |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public function isAvailable(): bool { |
175
|
|
|
return ($this->distributedCacheClass !== self::NULL_CACHE); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @see \OC\Memcache\Factory::createLocal() |
180
|
|
|
* @param string $prefix |
181
|
|
|
* @return ICache |
182
|
|
|
*/ |
183
|
|
|
public function createLowLatency(string $prefix = ''): ICache { |
184
|
|
|
return $this->createLocal($prefix); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Check if a local memory cache backend is available |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
public function isLocalCacheAvailable(): bool { |
193
|
|
|
return ($this->localCacheClass !== self::NULL_CACHE); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: