Update git submodules
[mediawiki.git] / includes / parser / RevisionOutputCache.php
blobbdce7d2c1529ff0c3d944ea17c5b64e463b090d6
1 <?php
2 /**
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
20 * @file
21 * @ingroup Cache Parser
24 namespace MediaWiki\Parser;
26 use CacheTime;
27 use IBufferingStatsdDataFactory;
28 use InvalidArgumentException;
29 use MediaWiki\Json\JsonCodec;
30 use MediaWiki\Revision\RevisionRecord;
31 use MediaWiki\Utils\MWTimestamp;
32 use ParserOptions;
33 use ParserOutput;
34 use Psr\Log\LoggerInterface;
35 use WANObjectCache;
37 /**
38 * Cache for ParserOutput objects.
39 * The cache is split per ParserOptions.
41 * @since 1.36
42 * @ingroup Cache Parser
44 class RevisionOutputCache {
46 /** @var string The name of this cache. Used as a root of the cache key. */
47 private $name;
49 /** @var WANObjectCache */
50 private $cache;
52 /**
53 * Anything cached prior to this is invalidated
55 * @var string
57 private $cacheEpoch;
59 /**
60 * Expiry time for cache entries.
62 * @var int
64 private $cacheExpiry;
66 /** @var JsonCodec */
67 private $jsonCodec;
69 /** @var IBufferingStatsdDataFactory */
70 private $stats;
72 /** @var LoggerInterface */
73 private $logger;
75 /**
76 * @param string $name
77 * @param WANObjectCache $cache
78 * @param int $cacheExpiry Expiry for ParserOutput in $cache.
79 * @param string $cacheEpoch Anything before this timestamp is invalidated
80 * @param JsonCodec $jsonCodec
81 * @param IBufferingStatsdDataFactory $stats
82 * @param LoggerInterface $logger
84 public function __construct(
85 string $name,
86 WANObjectCache $cache,
87 int $cacheExpiry,
88 string $cacheEpoch,
89 JsonCodec $jsonCodec,
90 IBufferingStatsdDataFactory $stats,
91 LoggerInterface $logger
92 ) {
93 $this->name = $name;
94 $this->cache = $cache;
95 $this->cacheExpiry = $cacheExpiry;
96 $this->cacheEpoch = $cacheEpoch;
97 $this->jsonCodec = $jsonCodec;
98 $this->stats = $stats;
99 $this->logger = $logger;
103 * @param string $metricSuffix
105 private function incrementStats( string $metricSuffix ) {
106 $metricSuffix = str_replace( '.', '_', $metricSuffix );
107 $this->stats->increment( "RevisionOutputCache.{$this->name}.{$metricSuffix}" );
111 * Get a key that will be used by this cache to store the content
112 * for a given page considering the given options and the array of
113 * used options.
115 * If there is a possibility the revision does not have a revision id, use
116 * makeParserOutputKeyOptionalRevId() instead.
118 * @warning The exact format of the key is considered internal and is subject
119 * to change, thus should not be used as storage or long-term caching key.
120 * This is intended to be used for logging or keying something transient.
122 * @param RevisionRecord $revision
123 * @param ParserOptions $options
124 * @param array|null $usedOptions currently ignored
125 * @return string
126 * @internal
128 public function makeParserOutputKey(
129 RevisionRecord $revision,
130 ParserOptions $options,
131 array $usedOptions = null
132 ): string {
133 $usedOptions = ParserOptions::allCacheVaryingOptions();
135 $revId = $revision->getId();
136 if ( !$revId ) {
137 // If RevId is null, this would probably be unsafe to use as a cache key.
138 throw new InvalidArgumentException( "Revision must have an id number" );
140 $hash = $options->optionsHash( $usedOptions );
141 return $this->cache->makeKey( $this->name, $revId, $hash );
145 * Get a key that will be used for locks or pool counter
147 * Similar to makeParserOutputKey except the revision id might be null,
148 * in which case it is unsafe to cache, but still needs a key for things like
149 * poolcounter.
151 * @warning The exact format of the key is considered internal and is subject
152 * to change, thus should not be used as storage or long-term caching key.
153 * This is intended to be used for logging or keying something transient.
155 * @param RevisionRecord $revision
156 * @param ParserOptions $options
157 * @param array|null $usedOptions currently ignored
158 * @return string
159 * @internal
161 public function makeParserOutputKeyOptionalRevId(
162 RevisionRecord $revision,
163 ParserOptions $options,
164 array $usedOptions = null
165 ): string {
166 $usedOptions = ParserOptions::allCacheVaryingOptions();
168 // revId may be null.
169 $revId = (string)$revision->getId();
170 $hash = $options->optionsHash( $usedOptions );
171 return $this->cache->makeKey( $this->name, $revId, $hash );
175 * Retrieve the ParserOutput from cache.
176 * false if not found or outdated.
178 * @param RevisionRecord $revision
179 * @param ParserOptions $parserOptions
181 * @return ParserOutput|false False on failure
183 public function get( RevisionRecord $revision, ParserOptions $parserOptions ) {
184 if ( $this->cacheExpiry <= 0 ) {
185 // disabled
186 return false;
189 if ( !$parserOptions->isSafeToCache() ) {
190 $this->incrementStats( 'miss.unsafe' );
191 return false;
194 $cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
195 $json = $this->cache->get( $cacheKey );
197 if ( $json === false ) {
198 $this->incrementStats( 'miss.absent' );
199 return false;
202 $output = $this->restoreFromJson( $json, $cacheKey, ParserOutput::class );
203 if ( $output === null ) {
204 $this->incrementStats( 'miss.unserialize' );
205 return false;
208 $cacheTime = (int)MWTimestamp::convert( TS_UNIX, $output->getCacheTime() );
209 $expiryTime = (int)MWTimestamp::convert( TS_UNIX, $this->cacheEpoch );
210 $expiryTime = max( $expiryTime, (int)MWTimestamp::now( TS_UNIX ) - $this->cacheExpiry );
212 if ( $cacheTime < $expiryTime ) {
213 $this->incrementStats( 'miss.expired' );
214 return false;
217 $this->logger->debug( 'old-revision cache hit' );
218 $this->incrementStats( 'hit' );
219 return $output;
223 * @param ParserOutput $output
224 * @param RevisionRecord $revision
225 * @param ParserOptions $parserOptions
226 * @param string|null $cacheTime TS_MW timestamp when the output was generated
228 public function save(
229 ParserOutput $output,
230 RevisionRecord $revision,
231 ParserOptions $parserOptions,
232 string $cacheTime = null
234 if ( !$output->hasText() ) {
235 throw new InvalidArgumentException( 'Attempt to cache a ParserOutput with no text set!' );
238 if ( $this->cacheExpiry <= 0 ) {
239 // disabled
240 return;
243 $cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
245 $output->setCacheTime( $cacheTime ?: wfTimestampNow() );
246 $output->setCacheRevisionId( $revision->getId() );
248 // Save the timestamp so that we don't have to load the revision row on view
249 $output->setTimestamp( $revision->getTimestamp() );
251 $msg = "Saved in RevisionOutputCache with key $cacheKey" .
252 " and timestamp $cacheTime" .
253 " and revision id {$revision->getId()}.";
255 $output->addCacheMessage( $msg );
257 // The ParserOutput might be dynamic and have been marked uncacheable by the parser.
258 $output->updateCacheExpiry( $this->cacheExpiry );
260 $expiry = $output->getCacheExpiry();
261 if ( $expiry <= 0 ) {
262 $this->incrementStats( 'save.uncacheable' );
263 return;
266 if ( !$parserOptions->isSafeToCache() ) {
267 $this->incrementStats( 'save.unsafe' );
268 return;
271 $json = $this->encodeAsJson( $output, $cacheKey );
272 if ( $json === null ) {
273 $this->incrementStats( 'save.nonserializable' );
274 return;
277 $this->cache->set( $cacheKey, $json, $expiry );
278 $this->incrementStats( 'save.success' );
282 * @param string $jsonData
283 * @param string $key
284 * @param string $expectedClass
285 * @return CacheTime|ParserOutput|null
287 private function restoreFromJson( string $jsonData, string $key, string $expectedClass ) {
288 try {
289 /** @var CacheTime $obj */
290 $obj = $this->jsonCodec->unserialize( $jsonData, $expectedClass );
291 return $obj;
292 } catch ( InvalidArgumentException $e ) {
293 $this->logger->error( 'Unable to unserialize JSON', [
294 'name' => $this->name,
295 'cache_key' => $key,
296 'message' => $e->getMessage()
297 ] );
298 return null;
303 * @param CacheTime $obj
304 * @param string $key
305 * @return string|null
307 private function encodeAsJson( CacheTime $obj, string $key ) {
308 try {
309 return $this->jsonCodec->serialize( $obj );
310 } catch ( InvalidArgumentException $e ) {
311 $this->logger->error( 'Unable to serialize JSON', [
312 'name' => $this->name,
313 'cache_key' => $key,
314 'message' => $e->getMessage(),
315 ] );
316 return null;