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