| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Parser; |
| 25 |
|
| 26 |
use InvalidArgumentException; |
| 27 |
use JsonException; |
| 28 |
use MediaWiki\Json\JsonCodec; |
| 29 |
use MediaWiki\Revision\RevisionRecord; |
| 30 |
use MediaWiki\Utils\MWTimestamp; |
| 31 |
use Psr\Log\LoggerInterface; |
| 32 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 33 |
use Wikimedia\Stats\StatsFactory; |
| 34 |
use Wikimedia\UUID\GlobalIdGenerator; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class RevisionOutputCache { |
| 44 |
|
| 45 |
|
| 46 |
private $name; |
| 47 |
|
| 48 |
|
| 49 |
private $cache; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private $cacheEpoch; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private $cacheExpiry; |
| 64 |
|
| 65 |
|
| 66 |
private $jsonCodec; |
| 67 |
|
| 68 |
|
| 69 |
private $stats; |
| 70 |
|
| 71 |
|
| 72 |
private $logger; |
| 73 |
|
| 74 |
private GlobalIdGenerator $globalIdGenerator; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function __construct( |
| 87 |
string $name, |
| 88 |
WANObjectCache $cache, |
| 89 |
int $cacheExpiry, |
| 90 |
string $cacheEpoch, |
| 91 |
JsonCodec $jsonCodec, |
| 92 |
StatsFactory $stats, |
| 93 |
LoggerInterface $logger, |
| 94 |
GlobalIdGenerator $globalIdGenerator |
| 95 |
) { |
| 96 |
$this->name = $name; |
| 97 |
$this->cache = $cache; |
| 98 |
$this->cacheExpiry = $cacheExpiry; |
| 99 |
$this->cacheEpoch = $cacheEpoch; |
| 100 |
$this->jsonCodec = $jsonCodec; |
| 101 |
$this->stats = $stats; |
| 102 |
$this->logger = $logger; |
| 103 |
$this->globalIdGenerator = $globalIdGenerator; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
private function incrementStats( string $status, ?string $reason = null ) { |
| 111 |
$metricSuffix = $reason ? "{$status}_{$reason}" : $status; |
| 112 |
|
| 113 |
$this->stats->getCounter( 'RevisionOutputCache_operation_total' ) |
| 114 |
->setLabel( 'name', $this->name ) |
| 115 |
->setLabel( 'status', $status ) |
| 116 |
->setLabel( 'reason', $reason ?: 'n/a' ) |
| 117 |
->copyToStatsdAt( "RevisionOutputCache.{$this->name}.{$metricSuffix}" ) |
| 118 |
->increment(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function makeParserOutputKey( |
| 140 |
RevisionRecord $revision, |
| 141 |
ParserOptions $options, |
| 142 |
?array $usedOptions = null |
| 143 |
): string { |
| 144 |
$usedOptions = ParserOptions::allCacheVaryingOptions(); |
| 145 |
|
| 146 |
$revId = $revision->getId(); |
| 147 |
if ( !$revId ) { |
| 148 |
|
| 149 |
throw new InvalidArgumentException( "Revision must have an id number" ); |
| 150 |
} |
| 151 |
$hash = $options->optionsHash( $usedOptions ); |
| 152 |
return $this->cache->makeKey( $this->name, $revId, $hash ); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public function makeParserOutputKeyOptionalRevId( |
| 173 |
RevisionRecord $revision, |
| 174 |
ParserOptions $options, |
| 175 |
?array $usedOptions = null |
| 176 |
): string { |
| 177 |
$usedOptions = ParserOptions::allCacheVaryingOptions(); |
| 178 |
|
| 179 |
|
| 180 |
$revId = (string)$revision->getId(); |
| 181 |
$hash = $options->optionsHash( $usedOptions ); |
| 182 |
return $this->cache->makeKey( $this->name, $revId, $hash ); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function get( RevisionRecord $revision, ParserOptions $parserOptions ) { |
| 195 |
if ( $this->cacheExpiry <= 0 ) { |
| 196 |
|
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
if ( !$parserOptions->isSafeToCache() ) { |
| 201 |
$this->incrementStats( 'miss', 'unsafe' ); |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
$cacheKey = $this->makeParserOutputKey( $revision, $parserOptions ); |
| 206 |
$json = $this->cache->get( $cacheKey ); |
| 207 |
|
| 208 |
if ( $json === false ) { |
| 209 |
$this->incrementStats( 'miss', 'absent' ); |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
$output = $this->restoreFromJson( $json, $cacheKey, ParserOutput::class ); |
| 214 |
if ( $output === null ) { |
| 215 |
$this->incrementStats( 'miss', 'unserialize' ); |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
$cacheTime = (int)MWTimestamp::convert( TS_UNIX, $output->getCacheTime() ); |
| 220 |
$expiryTime = (int)MWTimestamp::convert( TS_UNIX, $this->cacheEpoch ); |
| 221 |
$expiryTime = max( $expiryTime, (int)MWTimestamp::now( TS_UNIX ) - $this->cacheExpiry ); |
| 222 |
|
| 223 |
if ( $cacheTime < $expiryTime ) { |
| 224 |
$this->incrementStats( 'miss', 'expired' ); |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
$this->logger->debug( 'old-revision cache hit' ); |
| 229 |
$this->incrementStats( 'hit' ); |
| 230 |
return $output; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function save( |
| 240 |
ParserOutput $output, |
| 241 |
RevisionRecord $revision, |
| 242 |
ParserOptions $parserOptions, |
| 243 |
?string $cacheTime = null |
| 244 |
) { |
| 245 |
if ( !$output->hasText() ) { |
| 246 |
throw new InvalidArgumentException( 'Attempt to cache a ParserOutput with no text set!' ); |
| 247 |
} |
| 248 |
|
| 249 |
if ( $this->cacheExpiry <= 0 ) { |
| 250 |
|
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
$cacheKey = $this->makeParserOutputKey( $revision, $parserOptions ); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
if ( $cacheTime ) { |
| 261 |
$output->setCacheTime( $cacheTime ); |
| 262 |
} else { |
| 263 |
$cacheTime = $output->getCacheTime(); |
| 264 |
} |
| 265 |
if ( !$output->getCacheRevisionId() ) { |
| 266 |
$output->setCacheRevisionId( $revision->getId() ); |
| 267 |
} |
| 268 |
if ( !$output->getRenderId() ) { |
| 269 |
$output->setRenderId( $this->globalIdGenerator->newUUIDv1() ); |
| 270 |
} |
| 271 |
if ( !$output->getRevisionTimestamp() ) { |
| 272 |
$output->setRevisionTimestamp( $revision->getTimestamp() ); |
| 273 |
} |
| 274 |
|
| 275 |
$msg = "Saved in RevisionOutputCache with key $cacheKey" . |
| 276 |
" and timestamp $cacheTime" . |
| 277 |
" and revision id {$revision->getId()}."; |
| 278 |
|
| 279 |
$output->addCacheMessage( $msg ); |
| 280 |
|
| 281 |
|
| 282 |
$output->updateCacheExpiry( $this->cacheExpiry ); |
| 283 |
|
| 284 |
$expiry = $output->getCacheExpiry(); |
| 285 |
if ( $expiry <= 0 ) { |
| 286 |
$this->incrementStats( 'save', 'uncacheable' ); |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
if ( !$parserOptions->isSafeToCache() ) { |
| 291 |
$this->incrementStats( 'save', 'unsafe' ); |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
$json = $this->encodeAsJson( $output, $cacheKey ); |
| 296 |
if ( $json === null ) { |
| 297 |
$this->incrementStats( 'save', 'nonserializable' ); |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
$this->cache->set( $cacheKey, $json, $expiry ); |
| 302 |
$this->incrementStats( 'save', 'success' ); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
private function restoreFromJson( string $jsonData, string $key, string $expectedClass ) { |
| 312 |
try { |
| 313 |
|
| 314 |
$obj = $this->jsonCodec->deserialize( $jsonData, $expectedClass ); |
| 315 |
return $obj; |
| 316 |
} catch ( JsonException $e ) { |
| 317 |
$this->logger->error( 'Unable to deserialize JSON', [ |
| 318 |
'name' => $this->name, |
| 319 |
'cache_key' => $key, |
| 320 |
'message' => $e->getMessage() |
| 321 |
] ); |
| 322 |
return null; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
private function encodeAsJson( CacheTime $obj, string $key ) { |
| 332 |
try { |
| 333 |
return $this->jsonCodec->serialize( $obj ); |
| 334 |
} catch ( JsonException $e ) { |
| 335 |
$this->logger->error( 'Unable to serialize JSON', [ |
| 336 |
'name' => $this->name, |
| 337 |
'cache_key' => $key, |
| 338 |
'message' => $e->getMessage(), |
| 339 |
] ); |
| 340 |
return null; |
| 341 |
} |
| 342 |
} |
| 343 |
} |