update porting to new machine
[wikipedia-parser-hphp.git] / ParserCache.php
blob524d6be5b2ef270c3d12756d6c22a79e0bb070e6
1 <?php
2 /**
3 * @ingroup Cache Parser
4 * @todo document
5 */
6 class ParserCache {
7 /**
8 * Get an instance of this object
9 */
10 public static function singleton() {
11 static $instance;
12 if ( !isset( $instance ) ) {
13 global $parserMemc;
14 $instance = new ParserCache( $parserMemc );
16 return $instance;
19 /**
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, $popts ) {
30 global $wgRequest;
32 if( $popts instanceof User ) // It used to be getKey( &$article, &$user )
33 $popts = ParserOptions::newFromUser( $popts );
35 $user = $popts->mUser;
36 $printable = ( $popts->getIsPrintable() ) ? '!printable=1' : '';
37 $hash = $user->getPageRenderingHash();
38 if( !$article->mTitle->quickUserCan( 'edit' ) ) {
39 // section edit links are suppressed even if the user has them on
40 $edit = '!edit=0';
41 } else {
42 $edit = '';
44 $pageid = $article->getID();
45 $renderkey = (int)($wgRequest->getVal('action') == 'render');
46 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}{$edit}{$printable}" );
47 return $key;
50 function getETag( $article, $popts ) {
51 return 'W/"' . $this->getKey($article, $popts) . "--" . $article->mTouched. '"';
54 function getDirty( $article, $popts ) {
55 $key = $this->getKey( $article, $popts );
56 wfDebug( "Trying parser cache $key\n" );
57 $value = $this->mMemc->get( $key );
58 return is_object( $value ) ? $value : false;
61 function get( $article, $popts ) {
62 global $wgCacheEpoch;
63 wfProfileIn( __METHOD__ );
65 $value = $this->getDirty( $article, $popts );
66 if ( !$value ) {
67 wfDebug( "Parser cache miss.\n" );
68 wfIncrStats( "pcache_miss_absent" );
69 wfProfileOut( __METHOD__ );
70 return false;
73 wfDebug( "Found.\n" );
74 # Invalid if article has changed since the cache was made
75 $canCache = $article->checkTouched();
76 $cacheTime = $value->getCacheTime();
77 $touched = $article->mTouched;
78 if ( !$canCache || $value->expired( $touched ) ) {
79 if ( !$canCache ) {
80 wfIncrStats( "pcache_miss_invalid" );
81 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
82 } else {
83 wfIncrStats( "pcache_miss_expired" );
84 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
86 $value = false;
87 } else {
88 if ( isset( $value->mTimestamp ) ) {
89 $article->mTimestamp = $value->mTimestamp;
91 wfIncrStats( "pcache_hit" );
94 wfProfileOut( __METHOD__ );
95 return $value;
98 function save( $parserOutput, $article, $popts ){
99 global $wgParserCacheExpireTime;
100 $key = $this->getKey( $article, $popts );
102 if( $parserOutput->getCacheTime() != -1 ) {
104 $now = wfTimestampNow();
105 $parserOutput->setCacheTime( $now );
107 // Save the timestamp so that we don't have to load the revision row on view
108 $parserOutput->mTimestamp = $article->getTimestamp();
110 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
111 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
113 if( $parserOutput->containsOldMagic() ){
114 $expire = 3600; # 1 hour
115 } else {
116 $expire = $wgParserCacheExpireTime;
118 $this->mMemc->set( $key, $parserOutput, $expire );
120 } else {
121 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );