don't load so much stuff on parser cache hit (experimental)
[lhc/web/wiklou.git] / includes / ParserCache.php
1 <?php
2
3 class ParserCache
4 {
5 function getKey( &$article, &$user ) {
6 global $wgDBname;
7 $hash = $user->getPageRenderingHash();
8 $pageid = intval( $article->getID() );
9 $key = "$wgDBname:pcache:idhash:$pageid-$hash";
10 return $key;
11 }
12
13 function get( &$article, &$user ) {
14 global $wgMemc, $wgCacheEpoch;
15 $fname = "ParserCache::get";
16 wfProfileIn( $fname );
17
18 $hash = $user->getPageRenderingHash();
19 $pageid = intval( $article->getID() );
20 $key = $this->getKey( $article, $user );
21 wfDebug( "Trying parser cache $key\n" );
22 $value = $wgMemc->get( $key );
23 if ( $value ) {
24 wfDebug( "Found.\n" );
25 # Delete if article has changed since the cache was made
26 $canCache = $article->checkTouched();
27 $cacheTime = $value->getCacheTime();
28 if ( !$canCache || $value->getCacheTime() <= $touched || $cacheTime < $wgCacheEpoch ) {
29 if ( !$canCache ) {
30 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
31 } else {
32 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
33 }
34 $wgMemc->delete( $key );
35 $value = false;
36 }
37 } else {
38 $value = false;
39 }
40
41 wfProfileOut( $fname );
42 return $value;
43 }
44
45 function save( $parserOutput, &$article, &$user ){
46 global $wgMemc;
47
48 $key = $this->getKey( $article, $user );
49 $now = wfTimestampNow();
50 $parserOutput->setCacheTime( $now );
51 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
52
53 if( $parserOutput->containsOldMagic() ){
54 $expire = 3600; # 1 hour
55 } else {
56 $expire = 86400; # 1 day
57 }
58
59 $wgMemc->set( $key, $parserOutput, $expire );
60 }
61 }
62
63
64 ?>