| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Settings\Cache; |
| 4 |
|
| 5 |
use MediaWiki\Settings\SettingsBuilderException; |
| 6 |
use MediaWiki\Settings\Source\SettingsIncludeLocator; |
| 7 |
use MediaWiki\Settings\Source\SettingsSource; |
| 8 |
use Stringable; |
| 9 |
use Wikimedia\ObjectCache\BagOStuff; |
| 10 |
use Wikimedia\WaitConditionLoop; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class CachedSource implements Stringable, SettingsSource, SettingsIncludeLocator { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
private const TIMEOUT = 2; |
| 23 |
|
| 24 |
|
| 25 |
private $cache; |
| 26 |
|
| 27 |
|
| 28 |
private $source; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( |
| 40 |
BagOStuff $cache, |
| 41 |
CacheableSource $source |
| 42 |
) { |
| 43 |
$this->cache = $cache; |
| 44 |
$this->source = $source; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function load(): array { |
| 58 |
$key = $this->cache->makeGlobalKey( |
| 59 |
__CLASS__, |
| 60 |
$this->source->getHashKey() |
| 61 |
); |
| 62 |
|
| 63 |
$result = null; |
| 64 |
$loop = new WaitConditionLoop( |
| 65 |
function () use ( $key, &$result ) { |
| 66 |
$item = $this->cache->get( $key ); |
| 67 |
|
| 68 |
if ( $this->isValidHit( $item ) ) { |
| 69 |
if ( $this->isExpired( $item ) ) { |
| 70 |
|
| 71 |
|
| 72 |
if ( $this->source->allowsStaleLoad() ) { |
| 73 |
$result = $item['value']; |
| 74 |
} |
| 75 |
} else { |
| 76 |
$result = $item['value']; |
| 77 |
return WaitConditionLoop::CONDITION_REACHED; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if ( $this->cache->lock( $key, 0, self::TIMEOUT ) ) { |
| 82 |
try { |
| 83 |
$result = $this->loadAndCache( $key ); |
| 84 |
} catch ( SettingsBuilderException $e ) { |
| 85 |
if ( $result === null ) { |
| 86 |
|
| 87 |
|
| 88 |
throw $e; |
| 89 |
} |
| 90 |
} finally { |
| 91 |
$this->cache->unlock( $key ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return $result === null |
| 96 |
? WaitConditionLoop::CONDITION_CONTINUE |
| 97 |
: WaitConditionLoop::CONDITION_REACHED; |
| 98 |
}, |
| 99 |
self::TIMEOUT |
| 100 |
); |
| 101 |
|
| 102 |
if ( $loop->invoke() !== WaitConditionLoop::CONDITION_REACHED ) { |
| 103 |
throw new SettingsBuilderException( |
| 104 |
'Exceeded {timeout}s timeout attempting to load and cache source {source}', |
| 105 |
[ |
| 106 |
'timeout' => self::TIMEOUT, |
| 107 |
'source' => $this->source, |
| 108 |
] |
| 109 |
|
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
return $result; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function __toString(): string { |
| 123 |
return $this->source->__toString(); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
private function isValidHit( $item ): bool { |
| 136 |
return $item && |
| 137 |
is_array( $item ) && |
| 138 |
isset( $item['expiry'] ) && |
| 139 |
isset( $item['generation'] ) && |
| 140 |
isset( $item['value'] ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
private function isExpired( $item ): bool { |
| 153 |
return $item['expiry'] < microtime( true ) || |
| 154 |
$this->expiresEarly( $item, $this->source->getExpiryWeight() ); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
private function expiresEarly( array $item, float $weight ): bool { |
| 180 |
if ( $weight == 0 || !isset( $item['expiry'] ) || !isset( $item['generation'] ) ) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
$expiryOffset = |
| 188 |
$item['generation'] * |
| 189 |
$weight * |
| 190 |
log( random_int( 1, PHP_INT_MAX ) / PHP_INT_MAX ); |
| 191 |
|
| 192 |
return ( $item['expiry'] + $expiryOffset ) <= microtime( true ); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
private function loadAndCache( string $key ): array { |
| 203 |
$ttl = |
| 204 |
$this->source->allowsStaleLoad() |
| 205 |
? BagOStuff::TTL_INDEFINITE |
| 206 |
: $this->source->getExpiryTtl(); |
| 207 |
|
| 208 |
$item = $this->loadWithMetadata(); |
| 209 |
$this->cache->set( $key, $item, $ttl ); |
| 210 |
return $item['value']; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
private function loadWithMetadata(): array { |
| 220 |
$start = microtime( true ); |
| 221 |
$value = $this->source->load(); |
| 222 |
$finish = microtime( true ); |
| 223 |
|
| 224 |
return [ |
| 225 |
'value' => $value, |
| 226 |
'expiry' => $start + $this->source->getExpiryTtl(), |
| 227 |
'generation' => $finish - $start, |
| 228 |
]; |
| 229 |
} |
| 230 |
|
| 231 |
public function locateInclude( string $location ): string { |
| 232 |
if ( $this->source instanceof SettingsIncludeLocator ) { |
| 233 |
return $this->source->locateInclude( $location ); |
| 234 |
} else { |
| 235 |
|
| 236 |
return $location; |
| 237 |
} |
| 238 |
} |
| 239 |
} |