Make sure we get a good memCached object.
[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 function getKey( $article, $popts ) {
36 global $wgRequest;
37
38 if( $popts instanceof User ) // It used to be getKey( &$article, &$user )
39 $popts = ParserOptions::newFromUser( $popts );
40
41 $user = $popts->mUser;
42 $printable = ( $popts->getIsPrintable() ) ? '!printable=1' : '';
43 $hash = $user->getPageRenderingHash();
44 if( !$article->mTitle->quickUserCan( 'edit' ) ) {
45 // section edit links are suppressed even if the user has them on
46 $edit = '!edit=0';
47 } else {
48 $edit = '';
49 }
50 $pageid = $article->getID();
51 $renderkey = (int)($wgRequest->getVal('action') == 'render');
52 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}{$edit}{$printable}" );
53 return $key;
54 }
55
56 function getETag( $article, $popts ) {
57 return 'W/"' . $this->getKey($article, $popts) . "--" . $article->mTouched. '"';
58 }
59
60 function getDirty( $article, $popts ) {
61 $key = $this->getKey( $article, $popts );
62 wfDebug( "Trying parser cache $key\n" );
63 $value = $this->mMemc->get( $key );
64 return is_object( $value ) ? $value : false;
65 }
66
67 function get( $article, $popts ) {
68 global $wgCacheEpoch;
69 wfProfileIn( __METHOD__ );
70
71 $value = $this->getDirty( $article, $popts );
72 if ( !$value ) {
73 wfDebug( "Parser cache miss.\n" );
74 wfIncrStats( "pcache_miss_absent" );
75 wfProfileOut( __METHOD__ );
76 return false;
77 }
78
79 wfDebug( "Found.\n" );
80 # Invalid if article has changed since the cache was made
81 $canCache = $article->checkTouched();
82 $cacheTime = $value->getCacheTime();
83 $touched = $article->mTouched;
84 if ( !$canCache || $value->expired( $touched ) ) {
85 if ( !$canCache ) {
86 wfIncrStats( "pcache_miss_invalid" );
87 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
88 } else {
89 wfIncrStats( "pcache_miss_expired" );
90 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
91 }
92 $value = false;
93 } else {
94 if ( isset( $value->mTimestamp ) ) {
95 $article->mTimestamp = $value->mTimestamp;
96 }
97 wfIncrStats( "pcache_hit" );
98 }
99
100 wfProfileOut( __METHOD__ );
101 return $value;
102 }
103
104 function save( $parserOutput, $article, $popts ){
105 $key = $this->getKey( $article, $popts );
106 $expire = $parserOutput->getCacheExpiry();
107
108 if( $expire > 0 ) {
109 $now = wfTimestampNow();
110 $parserOutput->setCacheTime( $now );
111
112 // Save the timestamp so that we don't have to load the revision row on view
113 $parserOutput->mTimestamp = $article->getTimestamp();
114
115 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
116 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
117
118 $this->mMemc->set( $key, $parserOutput, $expire );
119
120 } else {
121 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
122 }
123 }
124
125 }