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 getETag( &$article, &$user ) {
32 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched
. '"';
35 function get( &$article, &$user ) {
37 $fname = 'ParserCache::get';
38 wfProfileIn( $fname );
40 $hash = $user->getPageRenderingHash();
41 $pageid = intval( $article->getID() );
42 $key = $this->getKey( $article, $user );
44 wfDebug( "Trying parser cache $key\n" );
45 $value = $this->mMemc
->get( $key );
46 if ( is_object( $value ) ) {
47 wfDebug( "Found.\n" );
48 # Delete if article has changed since the cache was made
49 $canCache = $article->checkTouched();
50 $cacheTime = $value->getCacheTime();
51 $touched = $article->mTouched
;
52 if ( !$canCache ||
$value->expired( $touched ) ) {
54 wfIncrStats( "pcache_miss_invalid" );
55 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
57 wfIncrStats( "pcache_miss_expired" );
58 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
60 $this->mMemc
->delete( $key );
64 wfIncrStats( "pcache_hit" );
67 wfDebug( "Parser cache miss.\n" );
68 wfIncrStats( "pcache_miss_absent" );
72 wfProfileOut( $fname );
76 function save( $parserOutput, &$article, &$user ){
77 $key = $this->getKey( $article, $user );
78 $now = wfTimestampNow();
79 $parserOutput->setCacheTime( $now );
80 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
81 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
83 if( $parserOutput->containsOldMagic() ){
84 $expire = 3600; # 1 hour
86 $expire = 86400; # 1 day
88 $this->mMemc
->set( $key, $parserOutput, $expire );