3 * @ingroup Cache Parser
10 * Get an instance of this object
12 public static function singleton() {
14 if ( !isset( $instance ) ) {
16 $instance = new ParserCache( $parserMemc );
22 * Setup a cache pathway with a given back-end storage mechanism.
23 * May be a memcached client or a BagOStuff derivative.
25 * @param $memCached Object
27 function __construct( $memCached ) {
30 $parserMemc = $memCached = wfGetParserCacheStorage();
32 $this->mMemc
= $memCached;
35 function getKey( $article, $popts ) {
38 if( $popts instanceof User
) // It used to be getKey( &$article, &$user )
39 $popts = ParserOptions
::newFromUser( $popts );
41 $user = $popts->mUser
;
42 $printable = ( $popts->getIsPrintable() ) ?
'!printable=1' : '';
43 $hash = $user->getPageRenderingHash();
44 if( !$article->mTitle
->quickUserCan( 'edit' ) ) {
45 // section edit links are suppressed even if the user has them on
50 $pageid = $article->getID();
51 $renderkey = (int)($wgRequest->getVal('action') == 'render');
52 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}{$edit}{$printable}" );
56 function getETag( $article, $popts ) {
57 return 'W/"' . $this->getKey($article, $popts) . "--" . $article->mTouched
. '"';
60 function getDirty( $article, $popts ) {
61 $key = $this->getKey( $article, $popts );
62 wfDebug( "Trying parser cache $key\n" );
63 $value = $this->mMemc
->get( $key );
64 return is_object( $value ) ?
$value : false;
67 function get( $article, $popts ) {
69 wfProfileIn( __METHOD__
);
71 $value = $this->getDirty( $article, $popts );
73 wfDebug( "Parser cache miss.\n" );
74 wfIncrStats( "pcache_miss_absent" );
75 wfProfileOut( __METHOD__
);
79 wfDebug( "Found.\n" );
80 # Invalid if article has changed since the cache was made
81 $canCache = $article->checkTouched();
82 $cacheTime = $value->getCacheTime();
83 $touched = $article->mTouched
;
84 if ( !$canCache ||
$value->expired( $touched ) ) {
86 wfIncrStats( "pcache_miss_invalid" );
87 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
89 wfIncrStats( "pcache_miss_expired" );
90 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
94 if ( isset( $value->mTimestamp
) ) {
95 $article->mTimestamp
= $value->mTimestamp
;
97 wfIncrStats( "pcache_hit" );
100 wfProfileOut( __METHOD__
);
104 function save( $parserOutput, $article, $popts ){
105 $key = $this->getKey( $article, $popts );
106 $expire = $parserOutput->getCacheExpiry();
109 $now = wfTimestampNow();
110 $parserOutput->setCacheTime( $now );
112 // Save the timestamp so that we don't have to load the revision row on view
113 $parserOutput->mTimestamp
= $article->getTimestamp();
115 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
116 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
118 $this->mMemc
->set( $key, $parserOutput, $expire );
121 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );