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 if( !$article->mTitle
->userCanEdit() ) {
27 // section edit links are suppressed even if the user has them on
32 $pageid = intval( $article->getID() );
33 $renderkey = (int)($action == 'render');
34 $key = "$wgDBname:pcache:idhash:$pageid-$renderkey!$hash$edit";
38 function getETag( &$article, &$user ) {
39 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched
. '"';
42 function get( &$article, &$user ) {
44 $fname = 'ParserCache::get';
45 wfProfileIn( $fname );
47 $hash = $user->getPageRenderingHash();
48 $pageid = intval( $article->getID() );
49 $key = $this->getKey( $article, $user );
51 wfDebug( "Trying parser cache $key\n" );
52 $value = $this->mMemc
->get( $key );
53 if ( is_object( $value ) ) {
54 wfDebug( "Found.\n" );
55 # Delete if article has changed since the cache was made
56 $canCache = $article->checkTouched();
57 $cacheTime = $value->getCacheTime();
58 $touched = $article->mTouched
;
59 if ( !$canCache ||
$value->expired( $touched ) ) {
61 wfIncrStats( "pcache_miss_invalid" );
62 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
64 wfIncrStats( "pcache_miss_expired" );
65 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
67 $this->mMemc
->delete( $key );
71 wfIncrStats( "pcache_hit" );
74 wfDebug( "Parser cache miss.\n" );
75 wfIncrStats( "pcache_miss_absent" );
79 wfProfileOut( $fname );
83 function save( $parserOutput, &$article, &$user ){
84 $key = $this->getKey( $article, $user );
85 $now = wfTimestampNow();
86 $parserOutput->setCacheTime( $now );
87 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
88 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
90 if( $parserOutput->containsOldMagic() ){
91 $expire = 3600; # 1 hour
93 $expire = 86400; # 1 day
95 $this->mMemc
->set( $key, $parserOutput, $expire );