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;
41 * @param $article Article
43 * @return mixed|string
45 protected function getParserOutputKey( $article, $hash ) {
48 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
49 $pageid = $article->getID();
50 $renderkey = (int)($wgRequest->getVal('action') == 'render');
52 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
57 * @param $article Article
58 * @return mixed|string
60 protected function getOptionsKey( $article ) {
61 $pageid = $article->getID();
62 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
66 * Provides an E-Tag suitable for the whole page. Note that $article
67 * is just the main wikitext. The E-Tag has to be unique to the whole
68 * page, even if the article itself is the same, so it uses the
69 * complete set of user options. We don't want to use the preference
70 * of a different user on a message just because it wasn't used in
71 * $article. For example give a Chinese interface to a user with
72 * English preferences. That's why we take into account *all* user
73 * options. (r70809 CR)
75 * @param $article Article
76 * @param $popts ParserOptions
78 function getETag( $article, $popts ) {
79 return 'W/"' . $this->getParserOutputKey( $article,
80 $popts->optionsHash( ParserOptions
::legacyOptions() ) ) .
81 "--" . $article->getTouched() . '"';
85 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
87 public function getDirty( $article, $popts ) {
88 $value = $this->get( $article, $popts, true );
89 return is_object( $value ) ?
$value : false;
93 * Used to provide a unique id for the PoolCounter.
94 * It would be preferable to have this code in get()
95 * instead of having Article looking in our internals.
97 * @param $article Article
98 * @param $popts ParserOptions
100 public function getKey( $article, $popts, $useOutdated = true ) {
101 global $wgCacheEpoch;
103 if( $popts instanceof User
) {
104 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
105 $popts = ParserOptions
::newFromUser( $popts );
108 // Determine the options which affect this article
109 $optionsKey = $this->mMemc
->get( $this->getOptionsKey( $article ) );
110 if ( $optionsKey != false ) {
111 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
112 wfIncrStats( "pcache_miss_expired" );
113 $cacheTime = $optionsKey->getCacheTime();
114 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
118 $usedOptions = $optionsKey->mUsedOptions
;
119 wfDebug( "Parser cache options found.\n" );
121 # TODO: Fail here $wgParserCacheExpireTime after deployment unless $useOutdated
123 $usedOptions = ParserOptions
::legacyOptions();
126 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
130 * Retrieve the ParserOutput from ParserCache.
131 * false if not found or outdated.
133 public function get( $article, $popts, $useOutdated = false ) {
134 global $wgCacheEpoch;
135 wfProfileIn( __METHOD__
);
137 $canCache = $article->checkTouched();
139 // It's a redirect now
140 wfProfileOut( __METHOD__
);
144 $touched = $article->getTouched();
146 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
147 if ( $parserOutputKey === false ) {
148 wfProfileOut( __METHOD__
);
152 $value = $this->mMemc
->get( $parserOutputKey );
154 wfDebug( "Parser cache miss.\n" );
155 wfIncrStats( "pcache_miss_absent" );
156 wfProfileOut( __METHOD__
);
160 wfDebug( "Found.\n" );
162 if ( !$useOutdated && $value->expired( $touched ) ) {
163 wfIncrStats( "pcache_miss_expired" );
164 $cacheTime = $value->getCacheTime();
165 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
168 if ( isset( $value->mTimestamp
) ) {
169 $article->mTimestamp
= $value->mTimestamp
;
171 wfIncrStats( "pcache_hit" );
174 wfProfileOut( __METHOD__
);
179 * @param $parserOutput ParserOutput
180 * @param $article Article
181 * @param $popts ParserOptions
184 public function save( $parserOutput, $article, $popts ) {
185 $expire = $parserOutput->getCacheExpiry();
188 $now = wfTimestampNow();
190 $optionsKey = new CacheTime
;
191 $optionsKey->mUsedOptions
= $parserOutput->getUsedOptions();
192 $optionsKey->updateCacheExpiry( $expire );
194 $optionsKey->setCacheTime( $now );
195 $parserOutput->setCacheTime( $now );
197 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
199 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( $optionsKey->mUsedOptions
) );
201 // Save the timestamp so that we don't have to load the revision row on view
202 $parserOutput->mTimestamp
= $article->getTimestamp();
204 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
205 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
207 // Save the parser output
208 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
210 // ...and its pointer
211 $this->mMemc
->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
213 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );