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