3 * Cache for outputs of the PHP parser
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Cache Parser
24 namespace MediaWiki\Parser
;
27 use InvalidArgumentException
;
29 use MediaWiki\Json\JsonCodec
;
30 use MediaWiki\Revision\RevisionRecord
;
31 use MediaWiki\Utils\MWTimestamp
;
33 use Psr\Log\LoggerInterface
;
35 use Wikimedia\Stats\StatsFactory
;
36 use Wikimedia\UUID\GlobalIdGenerator
;
39 * Cache for ParserOutput objects.
40 * The cache is split per ParserOptions.
43 * @ingroup Cache Parser
45 class RevisionOutputCache
{
47 /** @var string The name of this cache. Used as a root of the cache key. */
50 /** @var WANObjectCache */
54 * Anything cached prior to this is invalidated
61 * Expiry time for cache entries.
70 /** @var StatsFactory */
73 /** @var LoggerInterface */
76 private GlobalIdGenerator
$globalIdGenerator;
80 * @param WANObjectCache $cache
81 * @param int $cacheExpiry Expiry for ParserOutput in $cache.
82 * @param string $cacheEpoch Anything before this timestamp is invalidated
83 * @param JsonCodec $jsonCodec
84 * @param StatsFactory $stats
85 * @param LoggerInterface $logger
86 * @param GlobalIdGenerator $globalIdGenerator
88 public function __construct(
90 WANObjectCache
$cache,
95 LoggerInterface
$logger,
96 GlobalIdGenerator
$globalIdGenerator
99 $this->cache
= $cache;
100 $this->cacheExpiry
= $cacheExpiry;
101 $this->cacheEpoch
= $cacheEpoch;
102 $this->jsonCodec
= $jsonCodec;
103 $this->stats
= $stats;
104 $this->logger
= $logger;
105 $this->globalIdGenerator
= $globalIdGenerator;
109 * @param string $status e.g. hit, miss etc.
110 * @param string|null $reason
112 private function incrementStats( string $status, string $reason = null ) {
113 $metricSuffix = $reason ?
"{$status}_{$reason}" : $status;
115 $this->stats
->getCounter( 'RevisionOutputCache_operation_total' )
116 ->setLabel( 'name', $this->name
)
117 ->setLabel( 'status', $status )
118 ->setLabel( 'reason', $reason ?
: 'n/a' )
119 ->copyToStatsdAt( "RevisionOutputCache.{$this->name}.{$metricSuffix}" )
124 * Get a key that will be used by this cache to store the content
125 * for a given page considering the given options and the array of
128 * If there is a possibility the revision does not have a revision id, use
129 * makeParserOutputKeyOptionalRevId() instead.
131 * @warning The exact format of the key is considered internal and is subject
132 * to change, thus should not be used as storage or long-term caching key.
133 * This is intended to be used for logging or keying something transient.
135 * @param RevisionRecord $revision
136 * @param ParserOptions $options
137 * @param array|null $usedOptions currently ignored
141 public function makeParserOutputKey(
142 RevisionRecord
$revision,
143 ParserOptions
$options,
144 array $usedOptions = null
146 $usedOptions = ParserOptions
::allCacheVaryingOptions();
148 $revId = $revision->getId();
150 // If RevId is null, this would probably be unsafe to use as a cache key.
151 throw new InvalidArgumentException( "Revision must have an id number" );
153 $hash = $options->optionsHash( $usedOptions );
154 return $this->cache
->makeKey( $this->name
, $revId, $hash );
158 * Get a key that will be used for locks or pool counter
160 * Similar to makeParserOutputKey except the revision id might be null,
161 * in which case it is unsafe to cache, but still needs a key for things like
164 * @warning The exact format of the key is considered internal and is subject
165 * to change, thus should not be used as storage or long-term caching key.
166 * This is intended to be used for logging or keying something transient.
168 * @param RevisionRecord $revision
169 * @param ParserOptions $options
170 * @param array|null $usedOptions currently ignored
174 public function makeParserOutputKeyOptionalRevId(
175 RevisionRecord
$revision,
176 ParserOptions
$options,
177 array $usedOptions = null
179 $usedOptions = ParserOptions
::allCacheVaryingOptions();
181 // revId may be null.
182 $revId = (string)$revision->getId();
183 $hash = $options->optionsHash( $usedOptions );
184 return $this->cache
->makeKey( $this->name
, $revId, $hash );
188 * Retrieve the ParserOutput from cache.
189 * false if not found or outdated.
191 * @param RevisionRecord $revision
192 * @param ParserOptions $parserOptions
194 * @return ParserOutput|false False on failure
196 public function get( RevisionRecord
$revision, ParserOptions
$parserOptions ) {
197 if ( $this->cacheExpiry
<= 0 ) {
202 if ( !$parserOptions->isSafeToCache() ) {
203 $this->incrementStats( 'miss', 'unsafe' );
207 $cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
208 $json = $this->cache
->get( $cacheKey );
210 if ( $json === false ) {
211 $this->incrementStats( 'miss', 'absent' );
215 $output = $this->restoreFromJson( $json, $cacheKey, ParserOutput
::class );
216 if ( $output === null ) {
217 $this->incrementStats( 'miss', 'unserialize' );
221 $cacheTime = (int)MWTimestamp
::convert( TS_UNIX
, $output->getCacheTime() );
222 $expiryTime = (int)MWTimestamp
::convert( TS_UNIX
, $this->cacheEpoch
);
223 $expiryTime = max( $expiryTime, (int)MWTimestamp
::now( TS_UNIX
) - $this->cacheExpiry
);
225 if ( $cacheTime < $expiryTime ) {
226 $this->incrementStats( 'miss', 'expired' );
230 $this->logger
->debug( 'old-revision cache hit' );
231 $this->incrementStats( 'hit' );
236 * @param ParserOutput $output
237 * @param RevisionRecord $revision
238 * @param ParserOptions $parserOptions
239 * @param string|null $cacheTime TS_MW timestamp when the output was generated
241 public function save(
242 ParserOutput
$output,
243 RevisionRecord
$revision,
244 ParserOptions
$parserOptions,
245 string $cacheTime = null
247 if ( !$output->hasText() ) {
248 throw new InvalidArgumentException( 'Attempt to cache a ParserOutput with no text set!' );
251 if ( $this->cacheExpiry
<= 0 ) {
256 $cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
258 // Ensure cache properties are set in the ParserOutput
259 // T350538: These should be turned into assertions that the
260 // properties are already present (and the $cacheTime argument
263 $output->setCacheTime( $cacheTime );
265 $cacheTime = $output->getCacheTime();
267 if ( !$output->getCacheRevisionId() ) {
268 $output->setCacheRevisionId( $revision->getId() );
270 if ( !$output->getRenderId() ) {
271 $output->setRenderId( $this->globalIdGenerator
->newUUIDv1() );
273 if ( !$output->getRevisionTimestamp() ) {
274 $output->setRevisionTimestamp( $revision->getTimestamp() );
277 $msg = "Saved in RevisionOutputCache with key $cacheKey" .
278 " and timestamp $cacheTime" .
279 " and revision id {$revision->getId()}.";
281 $output->addCacheMessage( $msg );
283 // The ParserOutput might be dynamic and have been marked uncacheable by the parser.
284 $output->updateCacheExpiry( $this->cacheExpiry
);
286 $expiry = $output->getCacheExpiry();
287 if ( $expiry <= 0 ) {
288 $this->incrementStats( 'save', 'uncacheable' );
292 if ( !$parserOptions->isSafeToCache() ) {
293 $this->incrementStats( 'save', 'unsafe' );
297 $json = $this->encodeAsJson( $output, $cacheKey );
298 if ( $json === null ) {
299 $this->incrementStats( 'save', 'nonserializable' );
303 $this->cache
->set( $cacheKey, $json, $expiry );
304 $this->incrementStats( 'save', 'success' );
308 * @param string $jsonData
310 * @param string $expectedClass
311 * @return CacheTime|ParserOutput|null
313 private function restoreFromJson( string $jsonData, string $key, string $expectedClass ) {
315 /** @var CacheTime $obj */
316 $obj = $this->jsonCodec
->unserialize( $jsonData, $expectedClass );
318 } catch ( JsonException
$e ) {
319 $this->logger
->error( 'Unable to unserialize JSON', [
320 'name' => $this->name
,
322 'message' => $e->getMessage()
329 * @param CacheTime $obj
331 * @return string|null
333 private function encodeAsJson( CacheTime
$obj, string $key ) {
335 return $this->jsonCodec
->serialize( $obj );
336 } catch ( JsonException
$e ) {
337 $this->logger
->error( 'Unable to serialize JSON', [
338 'name' => $this->name
,
340 'message' => $e->getMessage(),