1f0458b0a725400396487ed4c7bed7c38eef7f38
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();
45 if( ! $popts->getEditSection() ) {
46 // section edit links have been suppressed
51 $pageid = $article->getID();
52 $renderkey = (int)($wgRequest->getVal('action') == 'render');
53 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}{$edit}{$printable}" );
57 function getETag( $article, $popts ) {
58 return 'W/"' . $this->getKey($article, $popts) . "--" . $article->mTouched
. '"';
61 function getDirty( $article, $popts ) {
62 $key = $this->getKey( $article, $popts );
63 wfDebug( "Trying parser cache $key\n" );
64 $value = $this->mMemc
->get( $key );
65 return is_object( $value ) ?
$value : false;
68 function get( $article, $popts ) {
70 wfProfileIn( __METHOD__
);
72 $value = $this->getDirty( $article, $popts );
74 wfDebug( "Parser cache miss.\n" );
75 wfIncrStats( "pcache_miss_absent" );
76 wfProfileOut( __METHOD__
);
80 wfDebug( "Found.\n" );
81 # Invalid if article has changed since the cache was made
82 $canCache = $article->checkTouched();
83 $cacheTime = $value->getCacheTime();
84 $touched = $article->mTouched
;
85 if ( !$canCache ||
$value->expired( $touched ) ) {
87 wfIncrStats( "pcache_miss_invalid" );
88 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
90 wfIncrStats( "pcache_miss_expired" );
91 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
95 if ( isset( $value->mTimestamp
) ) {
96 $article->mTimestamp
= $value->mTimestamp
;
98 wfIncrStats( "pcache_hit" );
101 wfProfileOut( __METHOD__
);
105 function save( $parserOutput, $article, $popts ){
106 $key = $this->getKey( $article, $popts );
107 $expire = $parserOutput->getCacheExpiry();
110 $now = wfTimestampNow();
111 $parserOutput->setCacheTime( $now );
113 // Save the timestamp so that we don't have to load the revision row on view
114 $parserOutput->mTimestamp
= $article->getTimestamp();
116 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
117 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
119 $this->mMemc
->set( $key, $parserOutput, $expire );
122 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );