712c664cdfdbcae868f05b85454e998a5917ec77
[lhc/web/wiklou.git] / includes / ParserCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 *
10 * @package MediaWiki
11 */
12 class ParserCache {
13 /**
14 * Get an instance of this object
15 */
16 function &singleton() {
17 static $instance;
18 if ( !isset( $instance ) ) {
19 global $parserMemc;
20 $instance = new ParserCache( $parserMemc );
21 }
22 return $instance;
23 }
24
25 /**
26 * Setup a cache pathway with a given back-end storage mechanism.
27 * May be a memcached client or a BagOStuff derivative.
28 *
29 * @param object $memCached
30 */
31 function ParserCache( &$memCached ) {
32 $this->mMemc =& $memCached;
33 }
34
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
40 $edit = '!edit=0';
41 } else {
42 $edit = '';
43 }
44 $pageid = intval( $article->getID() );
45 $renderkey = (int)($action == 'render');
46 $key = "$wgDBname:pcache:idhash:$pageid-$renderkey!$hash$edit";
47 return $key;
48 }
49
50 function getETag( &$article, &$user ) {
51 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched. '"';
52 }
53
54 function get( &$article, &$user ) {
55 global $wgCacheEpoch;
56 $fname = 'ParserCache::get';
57 wfProfileIn( $fname );
58
59 $hash = $user->getPageRenderingHash();
60 $pageid = intval( $article->getID() );
61 $key = $this->getKey( $article, $user );
62
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 ) ) {
72 if ( !$canCache ) {
73 wfIncrStats( "pcache_miss_invalid" );
74 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
75 } else {
76 wfIncrStats( "pcache_miss_expired" );
77 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
78 }
79 $this->mMemc->delete( $key );
80 $value = false;
81 } else {
82 if ( isset( $value->mTimestamp ) ) {
83 $article->mTimestamp = $value->mTimestamp;
84 }
85 wfIncrStats( "pcache_hit" );
86 }
87 } else {
88 wfDebug( "Parser cache miss.\n" );
89 wfIncrStats( "pcache_miss_absent" );
90 $value = false;
91 }
92
93 wfProfileOut( $fname );
94 return $value;
95 }
96
97 function save( $parserOutput, &$article, &$user ){
98 global $wgParserCacheExpireTime;
99 $key = $this->getKey( $article, $user );
100 $now = wfTimestampNow();
101 $parserOutput->setCacheTime( $now );
102
103 // Save the timestamp so that we don't have to load the revision row on view
104 $parserOutput->mTimestamp = $article->getTimestamp();
105
106 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
107 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
108
109 if( $parserOutput->containsOldMagic() ){
110 $expire = 3600; # 1 hour
111 } else {
112 $expire = $wgParserCacheExpireTime;
113 }
114 $this->mMemc->set( $key, $parserOutput, $expire );
115 }
116 }
117
118
119 ?>