2020-12-04 15:28:25 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Cache for outputs of the PHP parser
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Cache Parser
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Parser;
|
|
|
|
|
|
|
|
|
|
use CacheTime;
|
|
|
|
|
use IBufferingStatsdDataFactory;
|
|
|
|
|
use InvalidArgumentException;
|
2023-11-17 16:24:32 +00:00
|
|
|
use JsonException;
|
2020-12-04 15:28:25 +00:00
|
|
|
use MediaWiki\Json\JsonCodec;
|
|
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2023-08-19 03:35:06 +00:00
|
|
|
use MediaWiki\Utils\MWTimestamp;
|
2020-12-04 15:28:25 +00:00
|
|
|
use ParserOptions;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use WANObjectCache;
|
Add ParserOutput::{get,set}RenderId() and set render id in ContentRenderer
Set the render ID for each parse stored into cache so that we are able
to identify a specific parse when there are dependencies (for example
in an edit based on that parse). This is recorded as a property added
to the ParserOutput, not the parent CacheTime interface. Even though
the render ID is /related/ to the CacheTime interface, CacheTime is
also used directly as a parser cache key, and the UUID should not be
part of the lookup key.
In general we are trying to move the location where these cache
properties are set as early as possible, so we check at each location
to ensure we don't overwrite a previously-set value. Eventually we
can convert most of these checks into assertions that the cache
properties have already been set (T350538). The primary location for
setting cache properties is the ContentRenderer.
Moved setting the revision timestamp into ContentRenderer as well, as
it was set along the same code paths. An extra parameter was added to
ContentRenderer::getParserOutput() to support this.
Added merge code to ParserOutput::mergeInternalMetaDataFrom() which
should ensure that cache time, revision, timestamp, and render id are
all set properly when multiple slots are combined together in MCR.
In order to ensure the render ID is set on all codepaths we needed to
plumb the GlobalIdGenerator service into ContentRenderer, ParserCache,
ParserCacheFactory, and RevisionOutputCache. Eventually (T350538) it
should only be necessary in the ContentRenderer.
Bug: T350538
Bug: T349868
Followup-To: Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80
Change-Id: I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78
2023-09-14 16:11:20 +00:00
|
|
|
use Wikimedia\UUID\GlobalIdGenerator;
|
2020-12-04 15:28:25 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cache for ParserOutput objects.
|
|
|
|
|
* The cache is split per ParserOptions.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.36
|
|
|
|
|
* @ingroup Cache Parser
|
|
|
|
|
*/
|
|
|
|
|
class RevisionOutputCache {
|
|
|
|
|
|
|
|
|
|
/** @var string The name of this cache. Used as a root of the cache key. */
|
|
|
|
|
private $name;
|
|
|
|
|
|
|
|
|
|
/** @var WANObjectCache */
|
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Anything cached prior to this is invalidated
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $cacheEpoch;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Expiry time for cache entries.
|
|
|
|
|
*
|
2022-03-11 16:41:01 +00:00
|
|
|
* @var int
|
2020-12-04 15:28:25 +00:00
|
|
|
*/
|
|
|
|
|
private $cacheExpiry;
|
|
|
|
|
|
|
|
|
|
/** @var JsonCodec */
|
|
|
|
|
private $jsonCodec;
|
|
|
|
|
|
|
|
|
|
/** @var IBufferingStatsdDataFactory */
|
|
|
|
|
private $stats;
|
|
|
|
|
|
|
|
|
|
/** @var LoggerInterface */
|
|
|
|
|
private $logger;
|
|
|
|
|
|
Add ParserOutput::{get,set}RenderId() and set render id in ContentRenderer
Set the render ID for each parse stored into cache so that we are able
to identify a specific parse when there are dependencies (for example
in an edit based on that parse). This is recorded as a property added
to the ParserOutput, not the parent CacheTime interface. Even though
the render ID is /related/ to the CacheTime interface, CacheTime is
also used directly as a parser cache key, and the UUID should not be
part of the lookup key.
In general we are trying to move the location where these cache
properties are set as early as possible, so we check at each location
to ensure we don't overwrite a previously-set value. Eventually we
can convert most of these checks into assertions that the cache
properties have already been set (T350538). The primary location for
setting cache properties is the ContentRenderer.
Moved setting the revision timestamp into ContentRenderer as well, as
it was set along the same code paths. An extra parameter was added to
ContentRenderer::getParserOutput() to support this.
Added merge code to ParserOutput::mergeInternalMetaDataFrom() which
should ensure that cache time, revision, timestamp, and render id are
all set properly when multiple slots are combined together in MCR.
In order to ensure the render ID is set on all codepaths we needed to
plumb the GlobalIdGenerator service into ContentRenderer, ParserCache,
ParserCacheFactory, and RevisionOutputCache. Eventually (T350538) it
should only be necessary in the ContentRenderer.
Bug: T350538
Bug: T349868
Followup-To: Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80
Change-Id: I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78
2023-09-14 16:11:20 +00:00
|
|
|
private GlobalIdGenerator $globalIdGenerator;
|
|
|
|
|
|
2020-12-04 15:28:25 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param WANObjectCache $cache
|
|
|
|
|
* @param int $cacheExpiry Expiry for ParserOutput in $cache.
|
|
|
|
|
* @param string $cacheEpoch Anything before this timestamp is invalidated
|
|
|
|
|
* @param JsonCodec $jsonCodec
|
|
|
|
|
* @param IBufferingStatsdDataFactory $stats
|
|
|
|
|
* @param LoggerInterface $logger
|
Add ParserOutput::{get,set}RenderId() and set render id in ContentRenderer
Set the render ID for each parse stored into cache so that we are able
to identify a specific parse when there are dependencies (for example
in an edit based on that parse). This is recorded as a property added
to the ParserOutput, not the parent CacheTime interface. Even though
the render ID is /related/ to the CacheTime interface, CacheTime is
also used directly as a parser cache key, and the UUID should not be
part of the lookup key.
In general we are trying to move the location where these cache
properties are set as early as possible, so we check at each location
to ensure we don't overwrite a previously-set value. Eventually we
can convert most of these checks into assertions that the cache
properties have already been set (T350538). The primary location for
setting cache properties is the ContentRenderer.
Moved setting the revision timestamp into ContentRenderer as well, as
it was set along the same code paths. An extra parameter was added to
ContentRenderer::getParserOutput() to support this.
Added merge code to ParserOutput::mergeInternalMetaDataFrom() which
should ensure that cache time, revision, timestamp, and render id are
all set properly when multiple slots are combined together in MCR.
In order to ensure the render ID is set on all codepaths we needed to
plumb the GlobalIdGenerator service into ContentRenderer, ParserCache,
ParserCacheFactory, and RevisionOutputCache. Eventually (T350538) it
should only be necessary in the ContentRenderer.
Bug: T350538
Bug: T349868
Followup-To: Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80
Change-Id: I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78
2023-09-14 16:11:20 +00:00
|
|
|
* @param GlobalIdGenerator $globalIdGenerator
|
2020-12-04 15:28:25 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
string $name,
|
|
|
|
|
WANObjectCache $cache,
|
|
|
|
|
int $cacheExpiry,
|
|
|
|
|
string $cacheEpoch,
|
|
|
|
|
JsonCodec $jsonCodec,
|
|
|
|
|
IBufferingStatsdDataFactory $stats,
|
Add ParserOutput::{get,set}RenderId() and set render id in ContentRenderer
Set the render ID for each parse stored into cache so that we are able
to identify a specific parse when there are dependencies (for example
in an edit based on that parse). This is recorded as a property added
to the ParserOutput, not the parent CacheTime interface. Even though
the render ID is /related/ to the CacheTime interface, CacheTime is
also used directly as a parser cache key, and the UUID should not be
part of the lookup key.
In general we are trying to move the location where these cache
properties are set as early as possible, so we check at each location
to ensure we don't overwrite a previously-set value. Eventually we
can convert most of these checks into assertions that the cache
properties have already been set (T350538). The primary location for
setting cache properties is the ContentRenderer.
Moved setting the revision timestamp into ContentRenderer as well, as
it was set along the same code paths. An extra parameter was added to
ContentRenderer::getParserOutput() to support this.
Added merge code to ParserOutput::mergeInternalMetaDataFrom() which
should ensure that cache time, revision, timestamp, and render id are
all set properly when multiple slots are combined together in MCR.
In order to ensure the render ID is set on all codepaths we needed to
plumb the GlobalIdGenerator service into ContentRenderer, ParserCache,
ParserCacheFactory, and RevisionOutputCache. Eventually (T350538) it
should only be necessary in the ContentRenderer.
Bug: T350538
Bug: T349868
Followup-To: Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80
Change-Id: I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78
2023-09-14 16:11:20 +00:00
|
|
|
LoggerInterface $logger,
|
|
|
|
|
GlobalIdGenerator $globalIdGenerator
|
2020-12-04 15:28:25 +00:00
|
|
|
) {
|
|
|
|
|
$this->name = $name;
|
|
|
|
|
$this->cache = $cache;
|
|
|
|
|
$this->cacheExpiry = $cacheExpiry;
|
|
|
|
|
$this->cacheEpoch = $cacheEpoch;
|
|
|
|
|
$this->jsonCodec = $jsonCodec;
|
|
|
|
|
$this->stats = $stats;
|
|
|
|
|
$this->logger = $logger;
|
Add ParserOutput::{get,set}RenderId() and set render id in ContentRenderer
Set the render ID for each parse stored into cache so that we are able
to identify a specific parse when there are dependencies (for example
in an edit based on that parse). This is recorded as a property added
to the ParserOutput, not the parent CacheTime interface. Even though
the render ID is /related/ to the CacheTime interface, CacheTime is
also used directly as a parser cache key, and the UUID should not be
part of the lookup key.
In general we are trying to move the location where these cache
properties are set as early as possible, so we check at each location
to ensure we don't overwrite a previously-set value. Eventually we
can convert most of these checks into assertions that the cache
properties have already been set (T350538). The primary location for
setting cache properties is the ContentRenderer.
Moved setting the revision timestamp into ContentRenderer as well, as
it was set along the same code paths. An extra parameter was added to
ContentRenderer::getParserOutput() to support this.
Added merge code to ParserOutput::mergeInternalMetaDataFrom() which
should ensure that cache time, revision, timestamp, and render id are
all set properly when multiple slots are combined together in MCR.
In order to ensure the render ID is set on all codepaths we needed to
plumb the GlobalIdGenerator service into ContentRenderer, ParserCache,
ParserCacheFactory, and RevisionOutputCache. Eventually (T350538) it
should only be necessary in the ContentRenderer.
Bug: T350538
Bug: T349868
Followup-To: Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80
Change-Id: I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78
2023-09-14 16:11:20 +00:00
|
|
|
$this->globalIdGenerator = $globalIdGenerator;
|
2020-12-04 15:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $metricSuffix
|
|
|
|
|
*/
|
2023-02-05 19:21:50 +00:00
|
|
|
private function incrementStats( string $metricSuffix ) {
|
2020-12-04 15:28:25 +00:00
|
|
|
$metricSuffix = str_replace( '.', '_', $metricSuffix );
|
|
|
|
|
$this->stats->increment( "RevisionOutputCache.{$this->name}.{$metricSuffix}" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a key that will be used by this cache to store the content
|
|
|
|
|
* for a given page considering the given options and the array of
|
|
|
|
|
* used options.
|
|
|
|
|
*
|
2022-07-28 16:30:06 +00:00
|
|
|
* If there is a possibility the revision does not have a revision id, use
|
|
|
|
|
* makeParserOutputKeyOptionalRevId() instead.
|
|
|
|
|
*
|
2020-12-04 15:28:25 +00:00
|
|
|
* @warning The exact format of the key is considered internal and is subject
|
|
|
|
|
* to change, thus should not be used as storage or long-term caching key.
|
|
|
|
|
* This is intended to be used for logging or keying something transient.
|
|
|
|
|
*
|
|
|
|
|
* @param RevisionRecord $revision
|
|
|
|
|
* @param ParserOptions $options
|
|
|
|
|
* @param array|null $usedOptions currently ignored
|
|
|
|
|
* @return string
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
|
|
|
|
public function makeParserOutputKey(
|
|
|
|
|
RevisionRecord $revision,
|
|
|
|
|
ParserOptions $options,
|
|
|
|
|
array $usedOptions = null
|
|
|
|
|
): string {
|
|
|
|
|
$usedOptions = ParserOptions::allCacheVaryingOptions();
|
|
|
|
|
|
|
|
|
|
$revId = $revision->getId();
|
2022-07-28 16:30:06 +00:00
|
|
|
if ( !$revId ) {
|
|
|
|
|
// If RevId is null, this would probably be unsafe to use as a cache key.
|
|
|
|
|
throw new InvalidArgumentException( "Revision must have an id number" );
|
|
|
|
|
}
|
2020-12-04 15:28:25 +00:00
|
|
|
$hash = $options->optionsHash( $usedOptions );
|
2022-07-28 16:30:06 +00:00
|
|
|
return $this->cache->makeKey( $this->name, $revId, $hash );
|
|
|
|
|
}
|
2020-12-04 15:28:25 +00:00
|
|
|
|
2022-07-28 16:30:06 +00:00
|
|
|
/**
|
|
|
|
|
* Get a key that will be used for locks or pool counter
|
|
|
|
|
*
|
|
|
|
|
* Similar to makeParserOutputKey except the revision id might be null,
|
|
|
|
|
* in which case it is unsafe to cache, but still needs a key for things like
|
|
|
|
|
* poolcounter.
|
|
|
|
|
*
|
|
|
|
|
* @warning The exact format of the key is considered internal and is subject
|
|
|
|
|
* to change, thus should not be used as storage or long-term caching key.
|
|
|
|
|
* This is intended to be used for logging or keying something transient.
|
|
|
|
|
*
|
|
|
|
|
* @param RevisionRecord $revision
|
|
|
|
|
* @param ParserOptions $options
|
|
|
|
|
* @param array|null $usedOptions currently ignored
|
|
|
|
|
* @return string
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
|
|
|
|
public function makeParserOutputKeyOptionalRevId(
|
|
|
|
|
RevisionRecord $revision,
|
|
|
|
|
ParserOptions $options,
|
|
|
|
|
array $usedOptions = null
|
|
|
|
|
): string {
|
|
|
|
|
$usedOptions = ParserOptions::allCacheVaryingOptions();
|
|
|
|
|
|
|
|
|
|
// revId may be null.
|
|
|
|
|
$revId = (string)$revision->getId();
|
|
|
|
|
$hash = $options->optionsHash( $usedOptions );
|
2020-12-04 15:28:25 +00:00
|
|
|
return $this->cache->makeKey( $this->name, $revId, $hash );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the ParserOutput from cache.
|
|
|
|
|
* false if not found or outdated.
|
|
|
|
|
*
|
|
|
|
|
* @param RevisionRecord $revision
|
|
|
|
|
* @param ParserOptions $parserOptions
|
|
|
|
|
*
|
2022-07-31 00:02:18 +00:00
|
|
|
* @return ParserOutput|false False on failure
|
2020-12-04 15:28:25 +00:00
|
|
|
*/
|
|
|
|
|
public function get( RevisionRecord $revision, ParserOptions $parserOptions ) {
|
|
|
|
|
if ( $this->cacheExpiry <= 0 ) {
|
|
|
|
|
// disabled
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$parserOptions->isSafeToCache() ) {
|
2023-02-05 19:21:50 +00:00
|
|
|
$this->incrementStats( 'miss.unsafe' );
|
2020-12-04 15:28:25 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
|
|
|
|
|
$json = $this->cache->get( $cacheKey );
|
|
|
|
|
|
|
|
|
|
if ( $json === false ) {
|
2023-02-05 19:21:50 +00:00
|
|
|
$this->incrementStats( 'miss.absent' );
|
2020-12-04 15:28:25 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$output = $this->restoreFromJson( $json, $cacheKey, ParserOutput::class );
|
|
|
|
|
if ( $output === null ) {
|
2023-02-05 19:21:50 +00:00
|
|
|
$this->incrementStats( 'miss.unserialize' );
|
2020-12-04 15:28:25 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 22:12:49 +00:00
|
|
|
$cacheTime = (int)MWTimestamp::convert( TS_UNIX, $output->getCacheTime() );
|
|
|
|
|
$expiryTime = (int)MWTimestamp::convert( TS_UNIX, $this->cacheEpoch );
|
|
|
|
|
$expiryTime = max( $expiryTime, (int)MWTimestamp::now( TS_UNIX ) - $this->cacheExpiry );
|
|
|
|
|
|
|
|
|
|
if ( $cacheTime < $expiryTime ) {
|
2023-02-05 19:21:50 +00:00
|
|
|
$this->incrementStats( 'miss.expired' );
|
2020-12-04 15:28:25 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 22:20:44 +00:00
|
|
|
$this->logger->debug( 'old-revision cache hit' );
|
2023-02-05 19:21:50 +00:00
|
|
|
$this->incrementStats( 'hit' );
|
2020-12-04 15:28:25 +00:00
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ParserOutput $output
|
|
|
|
|
* @param RevisionRecord $revision
|
|
|
|
|
* @param ParserOptions $parserOptions
|
|
|
|
|
* @param string|null $cacheTime TS_MW timestamp when the output was generated
|
|
|
|
|
*/
|
|
|
|
|
public function save(
|
|
|
|
|
ParserOutput $output,
|
|
|
|
|
RevisionRecord $revision,
|
|
|
|
|
ParserOptions $parserOptions,
|
|
|
|
|
string $cacheTime = null
|
|
|
|
|
) {
|
|
|
|
|
if ( !$output->hasText() ) {
|
|
|
|
|
throw new InvalidArgumentException( 'Attempt to cache a ParserOutput with no text set!' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->cacheExpiry <= 0 ) {
|
|
|
|
|
// disabled
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
|
|
|
|
|
|
Add ParserOutput::{get,set}RenderId() and set render id in ContentRenderer
Set the render ID for each parse stored into cache so that we are able
to identify a specific parse when there are dependencies (for example
in an edit based on that parse). This is recorded as a property added
to the ParserOutput, not the parent CacheTime interface. Even though
the render ID is /related/ to the CacheTime interface, CacheTime is
also used directly as a parser cache key, and the UUID should not be
part of the lookup key.
In general we are trying to move the location where these cache
properties are set as early as possible, so we check at each location
to ensure we don't overwrite a previously-set value. Eventually we
can convert most of these checks into assertions that the cache
properties have already been set (T350538). The primary location for
setting cache properties is the ContentRenderer.
Moved setting the revision timestamp into ContentRenderer as well, as
it was set along the same code paths. An extra parameter was added to
ContentRenderer::getParserOutput() to support this.
Added merge code to ParserOutput::mergeInternalMetaDataFrom() which
should ensure that cache time, revision, timestamp, and render id are
all set properly when multiple slots are combined together in MCR.
In order to ensure the render ID is set on all codepaths we needed to
plumb the GlobalIdGenerator service into ContentRenderer, ParserCache,
ParserCacheFactory, and RevisionOutputCache. Eventually (T350538) it
should only be necessary in the ContentRenderer.
Bug: T350538
Bug: T349868
Followup-To: Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80
Change-Id: I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78
2023-09-14 16:11:20 +00:00
|
|
|
// Ensure cache properties are set in the ParserOutput
|
|
|
|
|
// T350538: These should be turned into assertions that the
|
|
|
|
|
// properties are already present (and the $cacheTime argument
|
|
|
|
|
// removed).
|
|
|
|
|
if ( $cacheTime ) {
|
|
|
|
|
$output->setCacheTime( $cacheTime );
|
|
|
|
|
} else {
|
|
|
|
|
$cacheTime = $output->getCacheTime();
|
|
|
|
|
}
|
|
|
|
|
if ( !$output->getCacheRevisionId() ) {
|
|
|
|
|
$output->setCacheRevisionId( $revision->getId() );
|
|
|
|
|
}
|
|
|
|
|
if ( !$output->getRenderId() ) {
|
|
|
|
|
$output->setRenderId( $this->globalIdGenerator->newUUIDv1() );
|
|
|
|
|
}
|
2023-11-06 21:25:07 +00:00
|
|
|
if ( !$output->getRevisionTimestamp() ) {
|
|
|
|
|
$output->setRevisionTimestamp( $revision->getTimestamp() );
|
Add ParserOutput::{get,set}RenderId() and set render id in ContentRenderer
Set the render ID for each parse stored into cache so that we are able
to identify a specific parse when there are dependencies (for example
in an edit based on that parse). This is recorded as a property added
to the ParserOutput, not the parent CacheTime interface. Even though
the render ID is /related/ to the CacheTime interface, CacheTime is
also used directly as a parser cache key, and the UUID should not be
part of the lookup key.
In general we are trying to move the location where these cache
properties are set as early as possible, so we check at each location
to ensure we don't overwrite a previously-set value. Eventually we
can convert most of these checks into assertions that the cache
properties have already been set (T350538). The primary location for
setting cache properties is the ContentRenderer.
Moved setting the revision timestamp into ContentRenderer as well, as
it was set along the same code paths. An extra parameter was added to
ContentRenderer::getParserOutput() to support this.
Added merge code to ParserOutput::mergeInternalMetaDataFrom() which
should ensure that cache time, revision, timestamp, and render id are
all set properly when multiple slots are combined together in MCR.
In order to ensure the render ID is set on all codepaths we needed to
plumb the GlobalIdGenerator service into ContentRenderer, ParserCache,
ParserCacheFactory, and RevisionOutputCache. Eventually (T350538) it
should only be necessary in the ContentRenderer.
Bug: T350538
Bug: T349868
Followup-To: Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80
Change-Id: I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78
2023-09-14 16:11:20 +00:00
|
|
|
}
|
2020-12-04 15:28:25 +00:00
|
|
|
|
|
|
|
|
$msg = "Saved in RevisionOutputCache with key $cacheKey" .
|
|
|
|
|
" and timestamp $cacheTime" .
|
|
|
|
|
" and revision id {$revision->getId()}.";
|
|
|
|
|
|
|
|
|
|
$output->addCacheMessage( $msg );
|
|
|
|
|
|
|
|
|
|
// The ParserOutput might be dynamic and have been marked uncacheable by the parser.
|
|
|
|
|
$output->updateCacheExpiry( $this->cacheExpiry );
|
|
|
|
|
|
|
|
|
|
$expiry = $output->getCacheExpiry();
|
|
|
|
|
if ( $expiry <= 0 ) {
|
2023-02-05 19:21:50 +00:00
|
|
|
$this->incrementStats( 'save.uncacheable' );
|
2020-12-04 15:28:25 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$parserOptions->isSafeToCache() ) {
|
2023-02-05 19:21:50 +00:00
|
|
|
$this->incrementStats( 'save.unsafe' );
|
2020-12-04 15:28:25 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$json = $this->encodeAsJson( $output, $cacheKey );
|
|
|
|
|
if ( $json === null ) {
|
2023-02-05 19:21:50 +00:00
|
|
|
$this->incrementStats( 'save.nonserializable' );
|
2020-12-04 15:28:25 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->cache->set( $cacheKey, $json, $expiry );
|
2023-02-05 19:21:50 +00:00
|
|
|
$this->incrementStats( 'save.success' );
|
2020-12-04 15:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $jsonData
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param string $expectedClass
|
|
|
|
|
* @return CacheTime|ParserOutput|null
|
|
|
|
|
*/
|
|
|
|
|
private function restoreFromJson( string $jsonData, string $key, string $expectedClass ) {
|
|
|
|
|
try {
|
|
|
|
|
/** @var CacheTime $obj */
|
|
|
|
|
$obj = $this->jsonCodec->unserialize( $jsonData, $expectedClass );
|
|
|
|
|
return $obj;
|
2023-11-17 16:24:32 +00:00
|
|
|
} catch ( JsonException $e ) {
|
2020-12-04 15:28:25 +00:00
|
|
|
$this->logger->error( 'Unable to unserialize JSON', [
|
|
|
|
|
'name' => $this->name,
|
|
|
|
|
'cache_key' => $key,
|
|
|
|
|
'message' => $e->getMessage()
|
|
|
|
|
] );
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param CacheTime $obj
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
private function encodeAsJson( CacheTime $obj, string $key ) {
|
|
|
|
|
try {
|
|
|
|
|
return $this->jsonCodec->serialize( $obj );
|
2023-11-17 16:24:32 +00:00
|
|
|
} catch ( JsonException $e ) {
|
2020-12-04 15:28:25 +00:00
|
|
|
$this->logger->error( 'Unable to serialize JSON', [
|
|
|
|
|
'name' => $this->name,
|
|
|
|
|
'cache_key' => $key,
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
] );
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|