remove typo extra line
[mediawiki.git] / includes / ParserCache.php
blob4447869b31fde0f9177ea23f2cd279de082bf753
1 <?php
2 /**
4 * @package MediaWiki
5 */
7 /**
9 * @package MediaWiki
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.
16 * @param object $memCached
18 function ParserCache( &$memCached ) {
19 $this->mMemc =& $memCached;
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;
30 function get( &$article, &$user ) {
31 global $wgCacheEpoch;
32 $fname = 'ParserCache::get';
33 wfProfileIn( $fname );
35 $hash = $user->getPageRenderingHash();
36 $pageid = intval( $article->getID() );
37 $key = $this->getKey( $article, $user );
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" );
55 $this->mMemc->delete( $key );
56 $value = false;
58 } else {
59 $this->incrStats( "pcache_hit" );
61 } else {
62 wfDebug( "Parser cache miss.\n" );
63 $this->incrStats( "pcache_miss_absent" );
64 $value = false;
67 wfProfileOut( $fname );
68 return $value;
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" );
78 if( $parserOutput->containsOldMagic() ){
79 $expire = 3600; # 1 hour
80 } else {
81 $expire = 86400; # 1 day
83 $this->mMemc->set( $key, $parserOutput, $expire );
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 );