Reverting for now this undiscussed and problematic change:
[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, $action;
25 $hash = $user->getPageRenderingHash();
26 $pageid = intval( $article->getID() );
27 $renderkey = (int)($action == 'render');
28 $key = "$wgDBname:pcache:idhash:$pageid-$renderkey!$hash";
29 return $key;
30 }
31
32 function getETag( &$article, &$user ) {
33 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched. '"';
34 }
35
36 function get( &$article, &$user ) {
37 global $wgCacheEpoch;
38 $fname = 'ParserCache::get';
39 wfProfileIn( $fname );
40
41 $hash = $user->getPageRenderingHash();
42 $pageid = intval( $article->getID() );
43 $key = $this->getKey( $article, $user );
44
45 wfDebug( "Trying parser cache $key\n" );
46 $value = $this->mMemc->get( $key );
47 if ( is_object( $value ) ) {
48 wfDebug( "Found.\n" );
49 # Delete if article has changed since the cache was made
50 $canCache = $article->checkTouched();
51 $cacheTime = $value->getCacheTime();
52 $touched = $article->mTouched;
53 if ( !$canCache || $value->expired( $touched ) ) {
54 if ( !$canCache ) {
55 wfIncrStats( "pcache_miss_invalid" );
56 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
57 } else {
58 wfIncrStats( "pcache_miss_expired" );
59 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
60 }
61 $this->mMemc->delete( $key );
62 $value = false;
63
64 } else {
65 wfIncrStats( "pcache_hit" );
66 }
67 } else {
68 wfDebug( "Parser cache miss.\n" );
69 wfIncrStats( "pcache_miss_absent" );
70 $value = false;
71 }
72
73 wfProfileOut( $fname );
74 return $value;
75 }
76
77 function save( $parserOutput, &$article, &$user ){
78 $key = $this->getKey( $article, $user );
79 $now = wfTimestampNow();
80 $parserOutput->setCacheTime( $now );
81 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
82 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
83
84 if( $parserOutput->containsOldMagic() ){
85 $expire = 3600; # 1 hour
86 } else {
87 $expire = 86400; # 1 day
88 }
89 $this->mMemc->set( $key, $parserOutput, $expire );
90 }
91 }
92
93
94 ?>