bug fix
[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 $touched = $article->mTouched;
29 if ( !$canCache || $value->getCacheTime() <= $touched || $cacheTime < $wgCacheEpoch ) {
30 if ( !$canCache ) {
31 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
32 } else {
33 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
34 }
35 $wgMemc->delete( $key );
36 $value = false;
37 }
38 } else {
39 $value = false;
40 }
41
42 wfProfileOut( $fname );
43 return $value;
44 }
45
46 function save( $parserOutput, &$article, &$user ){
47 global $wgMemc;
48
49 $key = $this->getKey( $article, $user );
50 $now = wfTimestampNow();
51 $parserOutput->setCacheTime( $now );
52 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
53
54 if( $parserOutput->containsOldMagic() ){
55 $expire = 3600; # 1 hour
56 } else {
57 $expire = 86400; # 1 day
58 }
59
60 $wgMemc->set( $key, $parserOutput, $expire );
61 }
62 }
63
64
65 ?>