3 * @ingroup Cache Parser
8 * Get an instance of this object
10 public static function &singleton() {
12 if ( !isset( $instance ) ) {
14 $instance = new ParserCache( $parserMemc );
20 * Setup a cache pathway with a given back-end storage mechanism.
21 * May be a memcached client or a BagOStuff derivative.
23 * @param object $memCached
25 function __construct( &$memCached ) {
26 $this->mMemc
=& $memCached;
29 function getKey( &$article, &$user ) {
31 $hash = $user->getPageRenderingHash();
32 if( !$article->mTitle
->quickUserCan( 'edit' ) ) {
33 // section edit links are suppressed even if the user has them on
38 $pageid = intval( $article->getID() );
39 $renderkey = (int)($action == 'render');
40 $key = wfMemcKey( 'pcache', 'idhash', "$pageid-$renderkey!$hash$edit" );
44 function getETag( &$article, &$user ) {
45 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched
. '"';
48 function get( &$article, &$user ) {
50 $fname = 'ParserCache::get';
51 wfProfileIn( $fname );
53 $key = $this->getKey( $article, $user );
55 wfDebug( "Trying parser cache $key\n" );
56 $value = $this->mMemc
->get( $key );
57 if ( is_object( $value ) ) {
58 wfDebug( "Found.\n" );
59 # Delete if article has changed since the cache was made
60 $canCache = $article->checkTouched();
61 $cacheTime = $value->getCacheTime();
62 $touched = $article->mTouched
;
63 if ( !$canCache ||
$value->expired( $touched ) ) {
65 wfIncrStats( "pcache_miss_invalid" );
66 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
68 wfIncrStats( "pcache_miss_expired" );
69 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
71 $this->mMemc
->delete( $key );
74 if ( isset( $value->mTimestamp
) ) {
75 $article->mTimestamp
= $value->mTimestamp
;
77 wfIncrStats( "pcache_hit" );
80 wfDebug( "Parser cache miss.\n" );
81 wfIncrStats( "pcache_miss_absent" );
85 wfProfileOut( $fname );
89 function save( $parserOutput, &$article, &$user ){
90 global $wgParserCacheExpireTime;
91 $key = $this->getKey( $article, $user );
93 if( $parserOutput->getCacheTime() != -1 ) {
95 $now = wfTimestampNow();
96 $parserOutput->setCacheTime( $now );
98 // Save the timestamp so that we don't have to load the revision row on view
99 $parserOutput->mTimestamp
= $article->getTimestamp();
101 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
102 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
104 if( $parserOutput->containsOldMagic() ){
105 $expire = 3600; # 1 hour
107 $expire = $wgParserCacheExpireTime;
109 $this->mMemc
->set( $key, $parserOutput, $expire );
112 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );