fbe6b912f5927611f1adb81dc63de09f5440e65d
14 * Get an instance of this object
16 function &singleton() {
18 if ( !isset( $instance ) ) {
20 $instance = new ParserCache( $parserMemc );
26 * Setup a cache pathway with a given back-end storage mechanism.
27 * May be a memcached client or a BagOStuff derivative.
29 * @param object $memCached
31 function ParserCache( &$memCached ) {
32 $this->mMemc
=& $memCached;
35 function getKey( &$article, &$user ) {
36 global $wgDBname, $action;
37 $hash = $user->getPageRenderingHash();
38 if( !$article->mTitle
->userCanEdit() ) {
39 // section edit links are suppressed even if the user has them on
44 $pageid = intval( $article->getID() );
45 $renderkey = (int)($action == 'render');
46 $key = "$wgDBname:pcache:idhash:$pageid-$renderkey!$hash$edit";
50 function getETag( &$article, &$user ) {
51 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched
. '"';
54 function get( &$article, &$user ) {
56 $fname = 'ParserCache::get';
57 wfProfileIn( $fname );
59 $hash = $user->getPageRenderingHash();
60 $pageid = intval( $article->getID() );
61 $key = $this->getKey( $article, $user );
63 wfDebug( "Trying parser cache $key\n" );
64 $value = $this->mMemc
->get( $key );
65 if ( is_object( $value ) ) {
66 wfDebug( "Found.\n" );
67 # Delete if article has changed since the cache was made
68 $canCache = $article->checkTouched();
69 $cacheTime = $value->getCacheTime();
70 $touched = $article->mTouched
;
71 if ( !$canCache ||
$value->expired( $touched ) ) {
73 wfIncrStats( "pcache_miss_invalid" );
74 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
76 wfIncrStats( "pcache_miss_expired" );
77 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
79 $this->mMemc
->delete( $key );
83 wfIncrStats( "pcache_hit" );
86 wfDebug( "Parser cache miss.\n" );
87 wfIncrStats( "pcache_miss_absent" );
91 wfProfileOut( $fname );
95 function save( $parserOutput, &$article, &$user ){
96 $key = $this->getKey( $article, $user );
97 $now = wfTimestampNow();
98 $parserOutput->setCacheTime( $now );
99 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
100 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
102 if( $parserOutput->containsOldMagic() ){
103 $expire = 3600; # 1 hour
105 $expire = 86400; # 1 day
107 $this->mMemc
->set( $key, $parserOutput, $expire );