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 ) {
25 $hash = $user->getPageRenderingHash();
26 $pageid = intval( $article->getID() );
27 $key = "$wgDBname:pcache:idhash:$pageid-$hash";
31 function get( &$article, &$user ) {
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 ) ) {
50 $this->incrStats( "pcache_miss_invalid" );
51 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
53 $this->incrStats( "pcache_miss_expired" );
54 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
56 $this->mMemc
->delete( $key );
60 $this->incrStats( "pcache_hit" );
63 wfDebug( "Parser cache miss.\n" );
64 $this->incrStats( "pcache_miss_absent" );
68 wfProfileOut( $fname );
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
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 );