28use InvalidArgumentException;
34use Psr\Log\LoggerInterface;
107 $metricSuffix = str_replace(
'.',
'_', $metricSuffix );
108 $this->stats->increment(
"RevisionOutputCache.{$this->name}.{$metricSuffix}" );
129 array $usedOptions =
null
133 $revId = $revision->
getId();
136 return $this->cache->makeKey( $this->name, $revId, $hash );
149 if ( $this->cacheExpiry <= 0 ) {
154 if ( !$parserOptions->isSafeToCache() ) {
155 $this->incrementStats( $revision,
'miss.unsafe' );
159 $cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
160 $json = $this->cache->get( $cacheKey );
162 if ( $json ===
false ) {
163 $this->incrementStats( $revision,
'miss.absent' );
167 $output = $this->restoreFromJson( $json, $cacheKey, ParserOutput::class );
168 if ( $output ===
null ) {
169 $this->incrementStats( $revision,
'miss.unserialize' );
173 $cacheTime = (int)MWTimestamp::convert( TS_UNIX, $output->getCacheTime() );
174 $expiryTime = (int)MWTimestamp::convert( TS_UNIX, $this->cacheEpoch );
175 $expiryTime = max( $expiryTime, (
int)MWTimestamp::now( TS_UNIX ) - $this->cacheExpiry );
177 if ( $cacheTime < $expiryTime ) {
178 $this->incrementStats( $revision,
'miss.expired' );
182 $this->logger->debug(
'output cache hit' );
183 $this->incrementStats( $revision,
'hit' );
197 string $cacheTime =
null
200 throw new InvalidArgumentException(
'Attempt to cache a ParserOutput with no text set!' );
203 if ( $this->cacheExpiry <= 0 ) {
208 $cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
216 $msg =
"Saved in RevisionOutputCache with key $cacheKey" .
217 " and timestamp $cacheTime" .
218 " and revision id {$revision->getId()}.";
226 if ( $expiry <= 0 ) {
227 $this->incrementStats( $revision,
'save.uncacheable' );
232 $this->incrementStats( $revision,
'save.unsafe' );
236 $json = $this->encodeAsJson( $output, $cacheKey );
237 if ( $json ===
null ) {
238 $this->incrementStats( $revision,
'save.nonserializable' );
242 $this->cache->set( $cacheKey, $json, $expiry );
243 $this->incrementStats( $revision,
'save.success' );
252 private function restoreFromJson(
string $jsonData,
string $key,
string $expectedClass ) {
255 $obj = $this->jsonCodec->unserialize( $jsonData, $expectedClass );
257 }
catch ( InvalidArgumentException $e ) {
258 $this->logger->error(
'Unable to unserialize JSON', [
259 'name' => $this->name,
261 'message' => $e->getMessage()
274 return $this->jsonCodec->serialize( $obj );
275 }
catch ( InvalidArgumentException $e ) {
276 $this->logger->error(
'Unable to serialize JSON', [
277 'name' => $this->name,
279 'message' => $e->getMessage(),
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Parser cache specific expiry check.
updateCacheExpiry( $seconds)
Sets the number of seconds after which this object should expire.
setCacheTime( $t)
setCacheTime() sets the timestamp expressing when the page has been rendered.
getCacheExpiry()
Returns the number of seconds after which this object should expire.
Library for creating and parsing MW-style timestamps.
Set options of the Parser.
isSafeToCache(array $usedOptions=null)
Test whether these options are safe to cache.
optionsHash( $forOptions, $title=null)
Generate a hash string with the values set on these ParserOptions for the keys given in the array.
hasText()
Returns true if text was passed to the constructor, or set using setText().
addCacheMessage(string $msg)
Adds a comment notice about cache state to the text of the page.
setTimestamp( $timestamp)
Multi-datacenter aware caching interface.
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.