3 * Cache for outputs of the PHP parser
9 * @ingroup Cache Parser
16 * Get an instance of this object
18 public static function singleton() {
20 if ( !isset( $instance ) ) {
22 $instance = new ParserCache( $parserMemc );
28 * Setup a cache pathway with a given back-end storage mechanism.
29 * May be a memcached client or a BagOStuff derivative.
31 * @param $memCached Object
33 function __construct( $memCached ) {
35 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
37 $this->mMemc
= $memCached;
40 protected function getParserOutputKey( $article, $hash ) {
43 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
44 $pageid = $article->getID();
45 $renderkey = (int)($wgRequest->getVal('action') == 'render');
47 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
51 protected function getOptionsKey( $article ) {
52 $pageid = $article->getID();
53 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
57 * Provides an E-Tag suitable for the whole page. Note that $article
58 * is just the main wikitext. The E-Tag has to be unique to the whole
59 * page, even if the article itself is the same, so it uses the
60 * complete set of user options. We don't want to use the preference
61 * of a different user on a message just because it wasn't used in
62 * $article. For example give a Chinese interface to a user with
63 * English preferences. That's why we take into account *all* user
64 * options. (r70809 CR)
66 function getETag( $article, $popts ) {
67 return 'W/"' . $this->getParserOutputKey( $article,
68 $popts->optionsHash( ParserOptions
::legacyOptions() ) ) .
69 "--" . $article->getTouched() . '"';
73 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
75 public function getDirty( $article, $popts ) {
76 $value = $this->get( $article, $popts, true );
77 return is_object( $value ) ?
$value : false;
81 * Used to provide a unique id for the PoolCounter.
82 * It would be preferable to have this code in get()
83 * instead of having Article looking in our internals.
85 * Precondition: $article->checkTouched() has been called.
87 public function getKey( $article, $popts, $useOutdated = true ) {
90 if( $popts instanceof User
) {
91 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
92 $popts = ParserOptions
::newFromUser( $popts );
95 // Determine the options which affect this article
96 $optionsKey = $this->mMemc
->get( $this->getOptionsKey( $article ) );
97 if ( $optionsKey != false ) {
98 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
99 wfIncrStats( "pcache_miss_expired" );
100 $cacheTime = $optionsKey->getCacheTime();
101 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
105 $usedOptions = $optionsKey->mUsedOptions
;
106 wfDebug( "Parser cache options found.\n" );
108 # TODO: Fail here $wgParserCacheExpireTime after deployment unless $useOutdated
110 $usedOptions = ParserOptions
::legacyOptions();
113 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
117 * Retrieve the ParserOutput from ParserCache.
118 * false if not found or outdated.
120 public function get( $article, $popts, $useOutdated = false ) {
121 global $wgCacheEpoch;
122 wfProfileIn( __METHOD__
);
124 $canCache = $article->checkTouched();
126 // It's a redirect now
127 wfProfileOut( __METHOD__
);
131 $touched = $article->getTouched();
133 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
134 if ( $parserOutputKey === false ) {
135 wfProfileOut( __METHOD__
);
139 $value = $this->mMemc
->get( $parserOutputKey );
141 wfDebug( "Parser cache miss.\n" );
142 wfIncrStats( "pcache_miss_absent" );
143 wfProfileOut( __METHOD__
);
147 wfDebug( "Found.\n" );
149 if ( !$useOutdated && $value->expired( $touched ) ) {
150 wfIncrStats( "pcache_miss_expired" );
151 $cacheTime = $value->getCacheTime();
152 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
155 if ( isset( $value->mTimestamp
) ) {
156 $article->mTimestamp
= $value->mTimestamp
;
158 wfIncrStats( "pcache_hit" );
161 wfProfileOut( __METHOD__
);
166 public function save( $parserOutput, $article, $popts ) {
167 $expire = $parserOutput->getCacheExpiry();
170 $now = wfTimestampNow();
172 $optionsKey = new CacheTime
;
173 $optionsKey->mUsedOptions
= $popts->usedOptions();
174 $optionsKey->updateCacheExpiry( $expire );
176 $optionsKey->setCacheTime( $now );
177 $parserOutput->setCacheTime( $now );
179 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
181 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( $optionsKey->mUsedOptions
) );
183 // Save the timestamp so that we don't have to load the revision row on view
184 $parserOutput->mTimestamp
= $article->getTimestamp();
186 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
187 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
189 // Save the parser output
190 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
192 // ...and its pointer
193 $this->mMemc
->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
195 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );