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, $action;
25 $hash = $user->getPageRenderingHash();
26 $pageid = intval( $article->getID() );
27 $renderkey = (int)($action == 'render');
28 $key = "$wgDBname:pcache:idhash:$pageid-$renderkey!$hash";
32 function getETag( &$article, &$user ) {
33 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched
. '"';
36 function get( &$article, &$user ) {
38 $fname = 'ParserCache::get';
39 wfProfileIn( $fname );
41 $hash = $user->getPageRenderingHash();
42 $pageid = intval( $article->getID() );
43 $key = $this->getKey( $article, $user );
45 wfDebug( "Trying parser cache $key\n" );
46 $value = $this->mMemc
->get( $key );
47 if ( is_object( $value ) ) {
48 wfDebug( "Found.\n" );
49 # Delete if article has changed since the cache was made
50 $canCache = $article->checkTouched();
51 $cacheTime = $value->getCacheTime();
52 $touched = $article->mTouched
;
53 if ( !$canCache ||
$value->expired( $touched ) ) {
55 wfIncrStats( "pcache_miss_invalid" );
56 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
58 wfIncrStats( "pcache_miss_expired" );
59 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
61 $this->mMemc
->delete( $key );
65 wfIncrStats( "pcache_hit" );
68 wfDebug( "Parser cache miss.\n" );
69 wfIncrStats( "pcache_miss_absent" );
73 wfProfileOut( $fname );
77 function save( $parserOutput, &$article, &$user ){
78 $key = $this->getKey( $article, $user );
79 $now = wfTimestampNow();
80 $parserOutput->setCacheTime( $now );
81 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
82 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
84 if( $parserOutput->containsOldMagic() ){
85 $expire = 3600; # 1 hour
87 $expire = 86400; # 1 day
89 $this->mMemc
->set( $key, $parserOutput, $expire );