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