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