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
19 public static function singleton() {
21 if ( !isset( $instance ) ) {
23 $instance = new ParserCache( $parserMemc );
29 * Setup a cache pathway with a given back-end storage mechanism.
30 * May be a memcached client or a BagOStuff derivative.
32 * @param $memCached Object
34 function __construct( $memCached ) {
36 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
38 $this->mMemc = $memCached;
41 protected function getParserOutputKey( $article, $hash ) {
44 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
45 $pageid = $article->getID();
46 $renderkey = (int)($wgRequest->getVal('action') == 'render');
48 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
52 protected function getOptionsKey( $article ) {
53 $pageid = $article->getID();
54 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
58 * Provides an E-Tag suitable for the whole page. Note that $article
59 * is just the main wikitext. The E-Tag has to be unique to the whole
60 * page, even if the article itself is the same, so it uses the
61 * complete set of user options. We don't want to use the preference
62 * of a different user on a message just because it wasn't used in
63 * $article. For example give a Chinese interface to a user with
64 * English preferences. That's why we take into account *all* user
65 * options. (r70809 CR)
67 function getETag( $article, $popts ) {
68 return 'W/"' . $this->getParserOutputKey( $article,
69 $popts->optionsHash( ParserOptions::legacyOptions() ) ) .
70 "--" . $article->getTouched() . '"';
74 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
76 public function getDirty( $article, $popts ) {
77 $value = $this->get( $article, $popts, true );
78 return is_object( $value ) ? $value : false;
82 * Used to provide a unique id for the PoolCounter.
83 * It would be preferable to have this code in get()
84 * instead of having Article looking in our internals.
86 public function getKey( $article, $popts, $useOutdated = true ) {
89 if( $popts instanceof User ) {
90 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
91 $popts = ParserOptions::newFromUser( $popts );
94 // Determine the options which affect this article
95 $optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
96 if ( $optionsKey != false ) {
97 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
98 wfIncrStats( "pcache_miss_expired" );
99 $cacheTime = $optionsKey->getCacheTime();
100 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
104 $usedOptions = $optionsKey->mUsedOptions;
105 wfDebug( "Parser cache options found.\n" );
107 if ( !$useOutdated && !self::try116cache ) {
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 );
140 if ( self::try116cache && !$value && strpos( $value, '*' ) !== -1 ) {
141 wfDebug( "New format parser cache miss.\n" );
142 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( ParserOptions::legacyOptions() ) );
143 $value = $this->mMemc->get( $parserOutputKey );
146 wfDebug( "Parser cache miss.\n" );
147 wfIncrStats( "pcache_miss_absent" );
148 wfProfileOut( __METHOD__ );
152 wfDebug( "Found.\n" );
154 if ( !$useOutdated && $value->expired( $touched ) ) {
155 wfIncrStats( "pcache_miss_expired" );
156 $cacheTime = $value->getCacheTime();
157 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
160 if ( isset( $value->mTimestamp ) ) {
161 $article->mTimestamp = $value->mTimestamp;
163 wfIncrStats( "pcache_hit" );
166 wfProfileOut( __METHOD__ );
171 public function save( $parserOutput, $article, $popts ) {
172 $expire = $parserOutput->getCacheExpiry();
175 $now = wfTimestampNow();
177 $optionsKey = new CacheTime;
178 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
179 $optionsKey->updateCacheExpiry( $expire );
181 $optionsKey->setCacheTime( $now );
182 $parserOutput->setCacheTime( $now );
184 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
186 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( $optionsKey->mUsedOptions ) );
188 // Save the timestamp so that we don't have to load the revision row on view
189 $parserOutput->mTimestamp = $article->getTimestamp();
191 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
192 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
194 // Save the parser output
195 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
197 // ...and its pointer
198 $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
200 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );