Debian Codegraph @

includes/parser/RevisionOutputCache.php

raw symbols SCIP
1 <?php
2 /**
3 * Cache for outputs of the PHP parser
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache Parser
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 * Cache for ParserOutput objects.
38 * The cache is split per ParserOptions.
39 *
40 * @since 1.36
41 * @ingroup Cache Parser
42 */
43 class RevisionOutputCache {
44
45 /** @var string The name of this cache. Used as a root of the cache key. */
46 private $name;
47
48 /** @var WANObjectCache */
49 private $cache;
50
51 /**
52 * Anything cached prior to this is invalidated
53 *
54 * @var string
55 */
56 private $cacheEpoch;
57
58 /**
59 * Expiry time for cache entries.
60 *
61 * @var int
62 */
63 private $cacheExpiry;
64
65 /** @var JsonCodec */
66 private $jsonCodec;
67
68 /** @var StatsFactory */
69 private $stats;
70
71 /** @var LoggerInterface */
72 private $logger;
73
74 private GlobalIdGenerator $globalIdGenerator;
75
76 /**
77 * @param string $name
78 * @param WANObjectCache $cache
79 * @param int $cacheExpiry Expiry for ParserOutput in $cache.
80 * @param string $cacheEpoch Anything before this timestamp is invalidated
81 * @param JsonCodec $jsonCodec
82 * @param StatsFactory $stats
83 * @param LoggerInterface $logger
84 * @param GlobalIdGenerator $globalIdGenerator
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 * @param string $status e.g. hit, miss etc.
108 * @param string|null $reason
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 * Get a key that will be used by this cache to store the content
123 * for a given page considering the given options and the array of
124 * used options.
125 *
126 * If there is a possibility the revision does not have a revision id, use
127 * makeParserOutputKeyOptionalRevId() instead.
128 *
129 * @warning The exact format of the key is considered internal and is subject
130 * to change, thus should not be used as storage or long-term caching key.
131 * This is intended to be used for logging or keying something transient.
132 *
133 * @param RevisionRecord $revision
134 * @param ParserOptions $options
135 * @param array|null $usedOptions currently ignored
136 * @return string
137 * @internal
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 // If RevId is null, this would probably be unsafe to use as a cache key.
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 * Get a key that will be used for locks or pool counter
157 *
158 * Similar to makeParserOutputKey except the revision id might be null,
159 * in which case it is unsafe to cache, but still needs a key for things like
160 * poolcounter.
161 *
162 * @warning The exact format of the key is considered internal and is subject
163 * to change, thus should not be used as storage or long-term caching key.
164 * This is intended to be used for logging or keying something transient.
165 *
166 * @param RevisionRecord $revision
167 * @param ParserOptions $options
168 * @param array|null $usedOptions currently ignored
169 * @return string
170 * @internal
171 */
172 public function makeParserOutputKeyOptionalRevId(
173 RevisionRecord $revision,
174 ParserOptions $options,
175 ?array $usedOptions = null
176 ): string {
177 $usedOptions = ParserOptions::allCacheVaryingOptions();
178
179 // revId may be null.
180 $revId = (string)$revision->getId();
181 $hash = $options->optionsHash( $usedOptions );
182 return $this->cache->makeKey( $this->name, $revId, $hash );
183 }
184
185 /**
186 * Retrieve the ParserOutput from cache.
187 * false if not found or outdated.
188 *
189 * @param RevisionRecord $revision
190 * @param ParserOptions $parserOptions
191 *
192 * @return ParserOutput|false False on failure
193 */
194 public function get( RevisionRecord $revision, ParserOptions $parserOptions ) {
195 if ( $this->cacheExpiry <= 0 ) {
196 // disabled
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 * @param ParserOutput $output
235 * @param RevisionRecord $revision
236 * @param ParserOptions $parserOptions
237 * @param string|null $cacheTime TS_MW timestamp when the output was generated
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 // disabled
251 return;
252 }
253
254 $cacheKey = $this->makeParserOutputKey( $revision, $parserOptions );
255
256 // Ensure cache properties are set in the ParserOutput
257 // T350538: These should be turned into assertions that the
258 // properties are already present (and the $cacheTime argument
259 // removed).
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 // The ParserOutput might be dynamic and have been marked uncacheable by the parser.
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 * @param string $jsonData
307 * @param string $key
308 * @param string $expectedClass
309 * @return CacheTime|ParserOutput|null
310 */
311 private function restoreFromJson( string $jsonData, string $key, string $expectedClass ) {
312 try {
313 /** @var CacheTime $obj */
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 * @param CacheTime $obj
328 * @param string $key
329 * @return string|null
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 }