* Added a test for a link with multiple pipes
[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 * Setup a cache pathway with a given back-end storage mechanism.
15 * May be a memcached client or a BagOStuff derivative.
16 *
17 * @param object $memCached
18 */
19 function ParserCache( &$memCached ) {
20 $this->mMemc =& $memCached;
21 }
22
23 function getKey( &$article, &$user ) {
24 global $wgDBname;
25 $hash = $user->getPageRenderingHash();
26 $pageid = intval( $article->getID() );
27 $key = "$wgDBname:pcache:idhash:$pageid-$hash";
28 return $key;
29 }
30
31 function get( &$article, &$user ) {
32 global $wgCacheEpoch;
33 $fname = 'ParserCache::get';
34 wfProfileIn( $fname );
35
36 $hash = $user->getPageRenderingHash();
37 $pageid = intval( $article->getID() );
38 $key = $this->getKey( $article, $user );
39
40 wfDebug( "Trying parser cache $key\n" );
41 $value = $this->mMemc->get( $key );
42 if ( is_object( $value ) ) {
43 wfDebug( "Found.\n" );
44 # Delete if article has changed since the cache was made
45 $canCache = $article->checkTouched();
46 $cacheTime = $value->getCacheTime();
47 $touched = $article->mTouched;
48 if ( !$canCache || $value->expired( $touched ) ) {
49 if ( !$canCache ) {
50 $this->incrStats( "pcache_miss_invalid" );
51 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
52 } else {
53 $this->incrStats( "pcache_miss_expired" );
54 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
55 }
56 $this->mMemc->delete( $key );
57 $value = false;
58
59 } else {
60 $this->incrStats( "pcache_hit" );
61 }
62 } else {
63 wfDebug( "Parser cache miss.\n" );
64 $this->incrStats( "pcache_miss_absent" );
65 $value = false;
66 }
67
68 wfProfileOut( $fname );
69 return $value;
70 }
71
72 function save( $parserOutput, &$article, &$user ){
73 $key = $this->getKey( $article, $user );
74 $now = wfTimestampNow();
75 $parserOutput->setCacheTime( $now );
76 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
77 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
78
79 if( $parserOutput->containsOldMagic() ){
80 $expire = 3600; # 1 hour
81 } else {
82 $expire = 86400; # 1 day
83 }
84 $this->mMemc->set( $key, $parserOutput, $expire );
85 }
86
87 function incrStats( $key ) {
88 global $wgDBname, $wgMemc;
89 $key = "$wgDBname:stats:$key";
90 if ( is_null( $wgMemc->incr( $key ) ) ) {
91 $wgMemc->add( $key, 1 );
92 }
93 }
94 }
95
96
97 ?>