* Fixed some phpdoc errors, see http://wikipedia.sf.net/doc/errors.html
[mediawiki.git] / includes / ParserCache.php
blobb156ccc7de600419fb421badd54189e968f50678
1 <?php
2 /**
4 * @package MediaWiki
5 * @subpackage Cache
6 */
8 /**
10 * @package MediaWiki
12 class ParserCache {
13 /**
14 * Setup a cache pathway with a given back-end storage mechanism.
15 * May be a memcached client or a BagOStuff derivative.
17 * @param object $memCached
19 function ParserCache( &$memCached ) {
20 $this->mMemc =& $memCached;
23 function getKey( &$article, &$user ) {
24 global $wgDBname;
25 $hash = $user->getPageRenderingHash();
26 $pageid = intval( $article->getID() );
27 $key = "$wgDBname:pcache:idhash:$pageid-$hash";
28 return $key;
31 function get( &$article, &$user ) {
32 global $wgCacheEpoch;
33 $fname = 'ParserCache::get';
34 wfProfileIn( $fname );
36 $hash = $user->getPageRenderingHash();
37 $pageid = intval( $article->getID() );
38 $key = $this->getKey( $article, $user );
40 wfDebug( "Trying parser cache $key\n" );
41 $value = $this->mMemc->get( $key );
42 if ( is_object( $value ) ) {
43 wfDebug( "Found.\n" );
44 # Delete if article has changed since the cache was made
45 $canCache = $article->checkTouched();
46 $cacheTime = $value->getCacheTime();
47 $touched = $article->mTouched;
48 if ( !$canCache || $value->expired( $touched ) ) {
49 if ( !$canCache ) {
50 $this->incrStats( "pcache_miss_invalid" );
51 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
52 } else {
53 $this->incrStats( "pcache_miss_expired" );
54 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
56 $this->mMemc->delete( $key );
57 $value = false;
59 } else {
60 $this->incrStats( "pcache_hit" );
62 } else {
63 wfDebug( "Parser cache miss.\n" );
64 $this->incrStats( "pcache_miss_absent" );
65 $value = false;
68 wfProfileOut( $fname );
69 return $value;
72 function save( $parserOutput, &$article, &$user ){
73 $key = $this->getKey( $article, $user );
74 $now = wfTimestampNow();
75 $parserOutput->setCacheTime( $now );
76 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
77 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
79 if( $parserOutput->containsOldMagic() ){
80 $expire = 3600; # 1 hour
81 } else {
82 $expire = 86400; # 1 day
84 $this->mMemc->set( $key, $parserOutput, $expire );
87 function incrStats( $key ) {
88 global $wgDBname, $wgMemc;
89 $key = "$wgDBname:stats:$key";
90 if ( is_null( $wgMemc->incr( $key ) ) ) {
91 $wgMemc->add( $key, 1 );