3 * Cache for outputs of the PHP parser
9 * @ingroup Cache Parser
14 const try116cache
= false; /* Only useful $wgParserCacheExpireTime after updating to 1.17 */
17 * Get an instance of this object
21 public static function singleton() {
23 if ( !isset( $instance ) ) {
25 $instance = new ParserCache( $parserMemc );
31 * Setup a cache pathway with a given back-end storage mechanism.
32 * May be a memcached client or a BagOStuff derivative.
34 * @param $memCached Object
36 protected function __construct( $memCached ) {
38 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
40 $this->mMemc
= $memCached;
44 * @param $article Article
46 * @return mixed|string
48 protected function getParserOutputKey( $article, $hash ) {
51 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
52 $pageid = $article->getID();
53 $renderkey = (int)($wgRequest->getVal('action') == 'render');
55 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
60 * @param $article Article
61 * @return mixed|string
63 protected function getOptionsKey( $article ) {
64 $pageid = $article->getID();
65 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
69 * Provides an E-Tag suitable for the whole page. Note that $article
70 * is just the main wikitext. The E-Tag has to be unique to the whole
71 * page, even if the article itself is the same, so it uses the
72 * complete set of user options. We don't want to use the preference
73 * of a different user on a message just because it wasn't used in
74 * $article. For example give a Chinese interface to a user with
75 * English preferences. That's why we take into account *all* user
76 * options. (r70809 CR)
78 * @param $article Article
79 * @param $popts ParserOptions
81 function getETag( $article, $popts ) {
82 return 'W/"' . $this->getParserOutputKey( $article,
83 $popts->optionsHash( ParserOptions
::legacyOptions() ) ) .
84 "--" . $article->getTouched() . '"';
88 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
89 * @param $article Article
90 * @param $popts ParserOptions
91 * @return ParserOutput|false
93 public function getDirty( $article, $popts ) {
94 $value = $this->get( $article, $popts, true );
95 return is_object( $value ) ?
$value : false;
99 * Used to provide a unique id for the PoolCounter.
100 * It would be preferable to have this code in get()
101 * instead of having Article looking in our internals.
103 * @param $article Article
104 * @param $popts ParserOptions
106 public function getKey( $article, $popts, $useOutdated = true ) {
107 global $wgCacheEpoch;
109 if( $popts instanceof User
) {
110 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
111 $popts = ParserOptions
::newFromUser( $popts );
114 // Determine the options which affect this article
115 $optionsKey = $this->mMemc
->get( $this->getOptionsKey( $article ) );
116 if ( $optionsKey != false ) {
117 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
118 wfIncrStats( "pcache_miss_expired" );
119 $cacheTime = $optionsKey->getCacheTime();
120 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
124 $usedOptions = $optionsKey->mUsedOptions
;
125 wfDebug( "Parser cache options found.\n" );
127 if ( !$useOutdated && !self
::try116cache
) {
130 $usedOptions = ParserOptions
::legacyOptions();
133 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
137 * Retrieve the ParserOutput from ParserCache.
138 * false if not found or outdated.
140 * @param $article Article
141 * @param $popts ParserOptions
142 * @param $useOutdated
144 * @return ParserOutput|false
146 public function get( $article, $popts, $useOutdated = false ) {
147 global $wgCacheEpoch;
148 wfProfileIn( __METHOD__
);
150 $canCache = $article->checkTouched();
152 // It's a redirect now
153 wfProfileOut( __METHOD__
);
157 $touched = $article->getTouched();
159 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
160 if ( $parserOutputKey === false ) {
161 wfProfileOut( __METHOD__
);
165 $value = $this->mMemc
->get( $parserOutputKey );
166 if ( self
::try116cache
&& !$value && strpos( $value, '*' ) !== -1 ) {
167 wfDebug( "New format parser cache miss.\n" );
168 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( ParserOptions
::legacyOptions() ) );
169 $value = $this->mMemc
->get( $parserOutputKey );
172 wfDebug( "Parser cache miss.\n" );
173 wfIncrStats( "pcache_miss_absent" );
174 wfProfileOut( __METHOD__
);
178 wfDebug( "Found.\n" );
180 if ( !$useOutdated && $value->expired( $touched ) ) {
181 wfIncrStats( "pcache_miss_expired" );
182 $cacheTime = $value->getCacheTime();
183 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
186 wfIncrStats( "pcache_hit" );
189 wfProfileOut( __METHOD__
);
194 * @param $parserOutput ParserOutput
195 * @param $article Article
196 * @param $popts ParserOptions
198 public function save( $parserOutput, $article, $popts ) {
199 $expire = $parserOutput->getCacheExpiry();
202 $now = wfTimestampNow();
204 $optionsKey = new CacheTime
;
205 $optionsKey->mUsedOptions
= $parserOutput->getUsedOptions();
206 $optionsKey->updateCacheExpiry( $expire );
208 $optionsKey->setCacheTime( $now );
209 $parserOutput->setCacheTime( $now );
211 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
213 $parserOutputKey = $this->getParserOutputKey( $article,
214 $popts->optionsHash( $optionsKey->mUsedOptions
) );
216 // Save the timestamp so that we don't have to load the revision row on view
217 $parserOutput->mTimestamp
= $article->getTimestamp();
219 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
220 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
222 // Save the parser output
223 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
225 // ...and its pointer
226 $this->mMemc
->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
228 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );