9 * Get an instance of this object
11 public static function &singleton() {
13 if ( !isset( $instance ) ) {
15 $instance = new ParserCache( $parserMemc );
21 * Setup a cache pathway with a given back-end storage mechanism.
22 * May be a memcached client or a BagOStuff derivative.
24 * @param object $memCached
26 function __construct( &$memCached ) {
27 $this->mMemc
=& $memCached;
30 function getKey( &$article, &$user ) {
32 $hash = $user->getPageRenderingHash();
33 if( !$article->mTitle
->quickUserCan( 'edit' ) ) {
34 // section edit links are suppressed even if the user has them on
39 $pageid = intval( $article->getID() );
40 $renderkey = (int)($action == 'render');
41 $key = wfMemcKey( 'pcache', 'idhash', "$pageid-$renderkey!$hash$edit" );
45 function getETag( &$article, &$user ) {
46 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched
. '"';
49 function get( &$article, &$user ) {
51 $fname = 'ParserCache::get';
52 wfProfileIn( $fname );
54 $key = $this->getKey( $article, $user );
56 wfDebug( "Trying parser cache $key\n" );
57 $value = $this->mMemc
->get( $key );
58 if ( is_object( $value ) ) {
59 wfDebug( "Found.\n" );
60 # Delete if article has changed since the cache was made
61 $canCache = $article->checkTouched();
62 $cacheTime = $value->getCacheTime();
63 $touched = $article->mTouched
;
64 if ( !$canCache ||
$value->expired( $touched ) ) {
66 wfIncrStats( "pcache_miss_invalid" );
67 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
69 wfIncrStats( "pcache_miss_expired" );
70 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
72 $this->mMemc
->delete( $key );
75 if ( isset( $value->mTimestamp
) ) {
76 $article->mTimestamp
= $value->mTimestamp
;
78 wfIncrStats( "pcache_hit" );
81 wfDebug( "Parser cache miss.\n" );
82 wfIncrStats( "pcache_miss_absent" );
86 wfProfileOut( $fname );
90 function save( $parserOutput, &$article, &$user ){
91 global $wgParserCacheExpireTime;
92 $key = $this->getKey( $article, $user );
94 if( $parserOutput->getCacheTime() != -1 ) {
96 $now = wfTimestampNow();
97 $parserOutput->setCacheTime( $now );
99 // Save the timestamp so that we don't have to load the revision row on view
100 $parserOutput->mTimestamp
= $article->getTimestamp();
102 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
103 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
105 if( $parserOutput->containsOldMagic() ){
106 $expire = 3600; # 1 hour
108 $expire = $wgParserCacheExpireTime;
110 $this->mMemc
->set( $key, $parserOutput, $expire );
113 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );