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;
42 * @param $article Article
44 * @return mixed|string
46 protected function getParserOutputKey( $article, $hash ) {
49 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
50 $pageid = $article->getID();
51 $renderkey = (int)($wgRequest->getVal('action') == 'render');
53 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
58 * @param $article Article
59 * @return mixed|string
61 protected function getOptionsKey( $article ) {
62 $pageid = $article->getID();
63 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
67 * Provides an E-Tag suitable for the whole page. Note that $article
68 * is just the main wikitext. The E-Tag has to be unique to the whole
69 * page, even if the article itself is the same, so it uses the
70 * complete set of user options. We don't want to use the preference
71 * of a different user on a message just because it wasn't used in
72 * $article. For example give a Chinese interface to a user with
73 * English preferences. That's why we take into account *all* user
74 * options. (r70809 CR)
76 * @param $article Article
77 * @param $popts ParserOptions
79 function getETag( $article, $popts ) {
80 return 'W/"' . $this->getParserOutputKey( $article,
81 $popts->optionsHash( ParserOptions
::legacyOptions() ) ) .
82 "--" . $article->getTouched() . '"';
86 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
88 public function getDirty( $article, $popts ) {
89 $value = $this->get( $article, $popts, true );
90 return is_object( $value ) ?
$value : false;
94 * Used to provide a unique id for the PoolCounter.
95 * It would be preferable to have this code in get()
96 * instead of having Article looking in our internals.
98 * @param $article Article
99 * @param $popts ParserOptions
101 public function getKey( $article, $popts, $useOutdated = true ) {
102 global $wgCacheEpoch;
104 if( $popts instanceof User
) {
105 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
106 $popts = ParserOptions
::newFromUser( $popts );
109 // Determine the options which affect this article
110 $optionsKey = $this->mMemc
->get( $this->getOptionsKey( $article ) );
111 if ( $optionsKey != false ) {
112 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
113 wfIncrStats( "pcache_miss_expired" );
114 $cacheTime = $optionsKey->getCacheTime();
115 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
119 $usedOptions = $optionsKey->mUsedOptions
;
120 wfDebug( "Parser cache options found.\n" );
122 if ( !$useOutdated && !self
::try116cache
) {
125 $usedOptions = ParserOptions
::legacyOptions();
128 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
132 * Retrieve the ParserOutput from ParserCache.
133 * false if not found or outdated.
135 public function get( $article, $popts, $useOutdated = false ) {
136 global $wgCacheEpoch;
137 wfProfileIn( __METHOD__
);
139 $canCache = $article->checkTouched();
141 // It's a redirect now
142 wfProfileOut( __METHOD__
);
146 $touched = $article->getTouched();
148 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
149 if ( $parserOutputKey === false ) {
150 wfProfileOut( __METHOD__
);
154 $value = $this->mMemc
->get( $parserOutputKey );
155 if ( self
::try116cache
&& !$value && strpos( $value, '*' ) !== -1 ) {
156 wfDebug( "New format parser cache miss.\n" );
157 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( ParserOptions
::legacyOptions() ) );
158 $value = $this->mMemc
->get( $parserOutputKey );
161 wfDebug( "Parser cache miss.\n" );
162 wfIncrStats( "pcache_miss_absent" );
163 wfProfileOut( __METHOD__
);
167 wfDebug( "Found.\n" );
169 if ( !$useOutdated && $value->expired( $touched ) ) {
170 wfIncrStats( "pcache_miss_expired" );
171 $cacheTime = $value->getCacheTime();
172 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
175 if ( isset( $value->mTimestamp
) ) {
176 $article->mTimestamp
= $value->mTimestamp
;
178 wfIncrStats( "pcache_hit" );
181 wfProfileOut( __METHOD__
);
186 * @param $parserOutput ParserOutput
187 * @param $article Article
188 * @param $popts ParserOptions
191 public function save( $parserOutput, $article, $popts ) {
192 $expire = $parserOutput->getCacheExpiry();
195 $now = wfTimestampNow();
197 $optionsKey = new CacheTime
;
198 $optionsKey->mUsedOptions
= $parserOutput->getUsedOptions();
199 $optionsKey->updateCacheExpiry( $expire );
201 $optionsKey->setCacheTime( $now );
202 $parserOutput->setCacheTime( $now );
204 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
206 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( $optionsKey->mUsedOptions
) );
208 // Save the timestamp so that we don't have to load the revision row on view
209 $parserOutput->mTimestamp
= $article->getTimestamp();
211 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
212 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
214 // Save the parser output
215 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
217 // ...and its pointer
218 $this->mMemc
->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
220 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );