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 ) {
24 $hash = $user->getPageRenderingHash();
25 $pageid = intval( $article->getID() );
26 $key = "$wgDBname:pcache:idhash:$pageid-$hash";
30 function get( &$article, &$user ) {
32 $fname = 'ParserCache::get';
33 wfProfileIn( $fname );
35 $hash = $user->getPageRenderingHash();
36 $pageid = intval( $article->getID() );
37 $key = $this->getKey( $article, $user );
38 wfDebug( "Trying parser cache $key\n" );
39 $value = $this->mMemc
->get( $key );
40 if ( is_object( $value ) ) {
41 wfDebug( "Found.\n" );
42 # Delete if article has changed since the cache was made
43 $canCache = $article->checkTouched();
44 $cacheTime = $value->getCacheTime();
45 $touched = $article->mTouched
;
46 if ( !$canCache ||
$value->expired( $touched ) ) {
48 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
50 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
52 $this->mMemc
->delete( $key );
56 wfDebug( "Parser cache miss.\n" );
60 wfProfileOut( $fname );
64 function save( $parserOutput, &$article, &$user ){
65 $key = $this->getKey( $article, $user );
66 $now = wfTimestampNow();
67 $parserOutput->setCacheTime( $now );
68 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
69 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
71 if( $parserOutput->containsOldMagic() ){
72 $expire = 3600; # 1 hour
74 $expire = 86400; # 1 day
76 $this->mMemc
->set( $key, $parserOutput, $expire );