3 * @ingroup Cache Parser
10 * Get an instance of this object
12 public static function singleton() {
14 if ( !isset( $instance ) ) {
16 $instance = new ParserCache( $parserMemc );
22 * Setup a cache pathway with a given back-end storage mechanism.
23 * May be a memcached client or a BagOStuff derivative.
25 * @param $memCached Object
27 function __construct( $memCached ) {
30 $parserMemc = $memCached = wfGetParserCacheStorage();
32 $this->mMemc
= $memCached;
35 protected function getParserOutputKey( $article, $hash ) {
38 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
39 $pageid = $article->getID();
40 $renderkey = (int)($wgRequest->getVal('action') == 'render');
42 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
46 protected function getOptionsKey( $article ) {
47 $pageid = $article->getID();
48 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
51 function getETag( $article, $popts ) {
52 return 'W/"' . $this->getParserOutputKey( $article,
53 $popts->optionsHash( ParserOptions
::legacyOptions() ) ) .
54 "--" . $article->mTouched
. '"';
58 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
60 public function getDirty( $article, $popts ) {
61 $value = $this->mMemc
->get( $article, $popts, true );
62 return is_object( $value ) ?
$value : false;
66 * Used to provide a unique id for the PoolCounter.
67 * It would be preferable to have this code in get()
68 * instead of having Article looking in our internals.
70 * Precondition: $article->checkTouched() has been called.
72 public function getKey( $article, $popts, $useOutdated = true ) {
75 // Determine the options which affect this article
76 $optionsKey = $this->mMemc
->get( $this->getOptionsKey( $article ) );
77 if ( $optionsKey !== false ) {
78 if ( !$useOutdated && $optionsKey->expired( $article->mTouched
) ) {
79 wfIncrStats( "pcache_miss_expired" );
80 $cacheTime = $optionsKey->getCacheTime();
81 wfDebug( "Parser options key expired, touched {$article->mTouched}, epoch $wgCacheEpoch, cached $cacheTime\n" );
85 $usedOptions = $optionsKey->mUsedOptions
;
86 wfDebug( "Parser cache options found.\n" );
88 # TODO: Fail here $wgParserCacheExpireTime after deployment unless $useOutdated
90 $usedOptions = ParserOptions
::legacyOptions();
93 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
97 * Retrieve the ParserOutput from ParserCache.
98 * false if not found or outdated.
100 public function get( $article, $popts, $useOutdated = false ) {
101 global $wgCacheEpoch;
102 wfProfileIn( __METHOD__
);
104 $canCache = $article->checkTouched();
106 // It's a redirect now
107 wfProfileOut( __METHOD__
);
111 // Having called checkTouched() ensures this will be loaded
112 $touched = $article->mTouched
;
114 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
115 if ( $parserOutputKey === false ) {
116 wfProfileOut( __METHOD__
);
120 $value = $this->mMemc
->get( $parserOutputKey );
122 wfDebug( "Parser cache miss.\n" );
123 wfIncrStats( "pcache_miss_absent" );
124 wfProfileOut( __METHOD__
);
128 wfDebug( "Found.\n" );
130 if ( !$useOutdated && $value->expired( $touched ) ) {
131 wfIncrStats( "pcache_miss_expired" );
132 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
135 if ( isset( $value->mTimestamp
) ) {
136 $article->mTimestamp
= $value->mTimestamp
;
138 wfIncrStats( "pcache_hit" );
141 wfProfileOut( __METHOD__
);
146 public function save( $parserOutput, $article, $popts ) {
147 $expire = $parserOutput->getCacheExpiry();
150 $now = wfTimestampNow();
152 $optionsKey = new CacheTime
;
153 $optionsKey->mUsedOptions
= $popts->usedOptions();
154 $optionsKey->updateCacheExpiry( $expire );
156 $optionsKey->setCacheTime( $now );
157 $parserOutput->setCacheTime( $now );
159 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
161 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( $optionsKey->mUsedOptions
) );
163 // Save the timestamp so that we don't have to load the revision row on view
164 $parserOutput->mTimestamp
= $article->getTimestamp();
166 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
167 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
169 // Save the parser output
170 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
172 // ...and its pointer
173 $this->mMemc
->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
175 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );