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