Use only the page relevant pieces in the parser cache key. Eg. two users with differe...
[lhc/web/wiklou.git] / includes / parser / ParserCache.php
1 <?php
2 /**
3 * @ingroup Cache Parser
4 * @todo document
5 */
6 class ParserCache {
7 private $mMemc;
8
9 /**
10 * Get an instance of this object
11 */
12 public static function singleton() {
13 static $instance;
14 if ( !isset( $instance ) ) {
15 global $parserMemc;
16 $instance = new ParserCache( $parserMemc );
17 }
18 return $instance;
19 }
20
21 /**
22 * Setup a cache pathway with a given back-end storage mechanism.
23 * May be a memcached client or a BagOStuff derivative.
24 *
25 * @param $memCached Object
26 */
27 function __construct( $memCached ) {
28 if ( !$memCached ) {
29 global $parserMemc;
30 $parserMemc = $memCached = wfGetParserCacheStorage();
31 }
32 $this->mMemc = $memCached;
33 }
34
35 protected function getParserOutputKey( $article, $hash ) {
36 global $wgRequest;
37
38 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
39 $pageid = $article->getID();
40 $renderkey = (int)($wgRequest->getVal('action') == 'render');
41
42 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
43 return $key;
44 }
45
46 protected function getOptionsKey( $article ) {
47 $pageid = $article->getID();
48 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
49 }
50
51 function getETag( $article, $popts ) {
52 return 'W/"' . $this->getParserOutputKey( $article,
53 $popts->optionsHash( ParserOptions::legacyOptions() ) ) .
54 "--" . $article->mTouched . '"';
55 }
56
57 /**
58 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
59 */
60 public function getDirty( $article, $popts ) {
61 $value = $this->mMemc->get( $article, $popts, true );
62 return is_object( $value ) ? $value : false;
63 }
64
65 /**
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.
69 *
70 * Precondition: $article->checkTouched() has been called.
71 */
72 public function getKey( $article, $popts, $useOutdated = true ) {
73 global $wgCacheEpoch;
74
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" );
82 return false;
83 }
84
85 $usedOptions = $optionsKey->mUsedOptions;
86 wfDebug( "Parser cache options found.\n" );
87 } else {
88 # TODO: Fail here $wgParserCacheExpireTime after deployment unless $useOutdated
89
90 $usedOptions = ParserOptions::legacyOptions();
91 }
92
93 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
94 }
95
96 /**
97 * Retrieve the ParserOutput from ParserCache.
98 * false if not found or outdated.
99 */
100 public function get( $article, $popts, $useOutdated = false ) {
101 global $wgCacheEpoch;
102 wfProfileIn( __METHOD__ );
103
104 $canCache = $article->checkTouched();
105 if ( !$canCache ) {
106 // It's a redirect now
107 wfProfileOut( __METHOD__ );
108 return false;
109 }
110
111 // Having called checkTouched() ensures this will be loaded
112 $touched = $article->mTouched;
113
114 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
115 if ( $parserOutputKey === false ) {
116 wfProfileOut( __METHOD__ );
117 return false;
118 }
119
120 $value = $this->mMemc->get( $parserOutputKey );
121 if ( !$value ) {
122 wfDebug( "Parser cache miss.\n" );
123 wfIncrStats( "pcache_miss_absent" );
124 wfProfileOut( __METHOD__ );
125 return false;
126 }
127
128 wfDebug( "Found.\n" );
129
130 if ( !$useOutdated && $value->expired( $touched ) ) {
131 wfIncrStats( "pcache_miss_expired" );
132 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
133 $value = false;
134 } else {
135 if ( isset( $value->mTimestamp ) ) {
136 $article->mTimestamp = $value->mTimestamp;
137 }
138 wfIncrStats( "pcache_hit" );
139 }
140
141 wfProfileOut( __METHOD__ );
142 return $value;
143 }
144
145
146 public function save( $parserOutput, $article, $popts ) {
147 $expire = $parserOutput->getCacheExpiry();
148
149 if( $expire > 0 ) {
150 $now = wfTimestampNow();
151
152 $optionsKey = new CacheTime;
153 $optionsKey->mUsedOptions = $popts->usedOptions();
154 $optionsKey->updateCacheExpiry( $expire );
155
156 $optionsKey->setCacheTime( $now );
157 $parserOutput->setCacheTime( $now );
158
159 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
160
161 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( $optionsKey->mUsedOptions ) );
162
163 // Save the timestamp so that we don't have to load the revision row on view
164 $parserOutput->mTimestamp = $article->getTimestamp();
165
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" );
168
169 // Save the parser output
170 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
171
172 // ...and its pointer
173 $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
174 } else {
175 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
176 }
177 }
178 }