12 * Get an instance of this object
14 public static function &singleton() {
16 if ( !isset( $instance ) ) {
18 $instance = new ParserCache( $parserMemc );
24 * Setup a cache pathway with a given back-end storage mechanism.
25 * May be a memcached client or a BagOStuff derivative.
27 * @param object $memCached
29 function __construct( &$memCached ) {
30 $this->mMemc
=& $memCached;
33 function getKey( &$article, &$user ) {
35 $hash = $user->getPageRenderingHash();
36 if( !$article->mTitle
->quickUserCan( 'edit' ) ) {
37 // section edit links are suppressed even if the user has them on
42 $pageid = intval( $article->getID() );
43 $renderkey = (int)($action == 'render');
44 $key = wfMemcKey( 'pcache', 'idhash', "$pageid-$renderkey!$hash$edit" );
48 function getETag( &$article, &$user ) {
49 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched
. '"';
52 function get( &$article, &$user ) {
54 $fname = 'ParserCache::get';
55 wfProfileIn( $fname );
57 $key = $this->getKey( $article, $user );
59 wfDebug( "Trying parser cache $key\n" );
60 $value = $this->mMemc
->get( $key );
61 if ( is_object( $value ) ) {
62 wfDebug( "Found.\n" );
63 # Delete if article has changed since the cache was made
64 $canCache = $article->checkTouched();
65 $cacheTime = $value->getCacheTime();
66 $touched = $article->mTouched
;
67 if ( !$canCache ||
$value->expired( $touched ) ) {
69 wfIncrStats( "pcache_miss_invalid" );
70 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
72 wfIncrStats( "pcache_miss_expired" );
73 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
75 $this->mMemc
->delete( $key );
78 if ( isset( $value->mTimestamp
) ) {
79 $article->mTimestamp
= $value->mTimestamp
;
81 wfIncrStats( "pcache_hit" );
84 wfDebug( "Parser cache miss.\n" );
85 wfIncrStats( "pcache_miss_absent" );
89 wfProfileOut( $fname );
93 function save( $parserOutput, &$article, &$user ){
94 global $wgParserCacheExpireTime;
95 $key = $this->getKey( $article, $user );
97 if( $parserOutput->getCacheTime() != -1 ) {
99 $now = wfTimestampNow();
100 $parserOutput->setCacheTime( $now );
102 // Save the timestamp so that we don't have to load the revision row on view
103 $parserOutput->mTimestamp
= $article->getTimestamp();
105 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
106 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
108 if( $parserOutput->containsOldMagic() ){
109 $expire = 3600; # 1 hour
111 $expire = $wgParserCacheExpireTime;
113 $this->mMemc
->set( $key, $parserOutput, $expire );
116 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );