* Respect read-only mode on block removals
[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 * Setup a cache pathway with a given back-end storage mechanism.
14 * May be a memcached client or a BagOStuff derivative.
15 *
16 * @param object $memCached
17 */
18 function ParserCache( &$memCached ) {
19 $this->mMemc =& $memCached;
20 }
21
22 function getKey( &$article, &$user ) {
23 global $wgDBname;
24 $hash = $user->getPageRenderingHash();
25 $pageid = intval( $article->getID() );
26 $key = "$wgDBname:pcache:idhash:$pageid-$hash";
27 return $key;
28 }
29
30 function get( &$article, &$user ) {
31 global $wgCacheEpoch;
32 $fname = 'ParserCache::get';
33 wfProfileIn( $fname );
34
35 $hash = $user->getPageRenderingHash();
36 $pageid = intval( $article->getID() );
37 $key = $this->getKey( $article, $user );
38
39 wfDebug( "Trying parser cache $key\n" );
40 $value = $this->mMemc->get( $key );
41 if ( is_object( $value ) ) {
42 wfDebug( "Found.\n" );
43 # Delete if article has changed since the cache was made
44 $canCache = $article->checkTouched();
45 $cacheTime = $value->getCacheTime();
46 $touched = $article->mTouched;
47 if ( !$canCache || $value->expired( $touched ) ) {
48 if ( !$canCache ) {
49 $this->incrStats( "pcache_miss_invalid" );
50 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
51 } else {
52 $this->incrStats( "pcache_miss_expired" );
53 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
54 }
55 $this->mMemc->delete( $key );
56 $value = false;
57
58 } else {
59 $this->incrStats( "pcache_hit" );
60 }
61 } else {
62 wfDebug( "Parser cache miss.\n" );
63 $this->incrStats( "pcache_miss_absent" );
64 $value = false;
65 }
66
67 wfProfileOut( $fname );
68 return $value;
69 }
70
71 function save( $parserOutput, &$article, &$user ){
72 $key = $this->getKey( $article, $user );
73 $now = wfTimestampNow();
74 $parserOutput->setCacheTime( $now );
75 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
76 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
77
78 if( $parserOutput->containsOldMagic() ){
79 $expire = 3600; # 1 hour
80 } else {
81 $expire = 86400; # 1 day
82 }
83 $this->mMemc->set( $key, $parserOutput, $expire );
84 }
85
86 function incrStats( $key ) {
87 global $wgDBname, $wgMemc;
88 $key = "$wgDBname:stats:$key";
89 if ( is_null( $wgMemc->incr( $key ) ) ) {
90 $wgMemc->add( $key, 1 );
91 }
92 }
93 }
94
95
96 ?>