From a42101c0a046d65b2d03d52c62b5adbb2e628c07 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Thu, 27 May 2004 15:24:04 +0000 Subject: [PATCH] Parser cache moved to memcached --- includes/Article.php | 21 ++++------- includes/Parser.php | 4 +++ includes/ParserCache.php | 77 ++++++++++++++-------------------------- 3 files changed, 38 insertions(+), 64 deletions(-) diff --git a/includes/Article.php b/includes/Article.php index 6ccdb7bb2a..710508f04a 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -1512,6 +1512,10 @@ class Article { return false; } } + + function getTouched() { + return $this->mTouched; + } /* static */ function incViewCount( $id ) { @@ -1567,7 +1571,7 @@ class Article { # This is called on page move and undelete, as well as edit /* static */ function onArticleCreate($title_obj){ - global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid, $wgDeferredUpdateList; + global $wgEnablePersistentLC, $wgUseSquid, $wgDeferredUpdateList; $titles = $title_obj->getBrokenLinksTo(); @@ -1585,31 +1589,20 @@ class Article { if ( $wgEnablePersistentLC ) { LinkCache::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() ); } - - # Clear parser cache (not really used) - if ( $wgEnableParserCache ) { - OutputPage::parsercacheClearBrokenLinksTo( $title_obj->getPrefixedDBkey() ); - } } /* static */ function onArticleDelete($title_obj){ - global $wgEnablePersistentLC, $wgEnableParserCache; + global $wgEnablePersistentLC; if ( $wgEnablePersistentLC ) { LinkCache::linksccClearLinksTo( $title_obj->getArticleID() ); } - if ( $wgEnableParserCache ) { - OutputPage::parsercacheClearLinksTo( $title_obj->getArticleID() ); - } } /* static */ function onArticleEdit($title_obj){ - global $wgEnablePersistentLC, $wgEnableParserCache; + global $wgEnablePersistentLC; if ( $wgEnablePersistentLC ) { LinkCache::linksccClearPage( $title_obj->getArticleID() ); } - if ( $wgEnableParserCache ) { - OutputPage::parsercacheClearPage( $title_obj->getArticleID(), $title_obj->getNamespace() ); - } } } diff --git a/includes/Parser.php b/includes/Parser.php index d5bbc156f6..200c8f4ae4 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -1976,6 +1976,7 @@ class Parser class ParserOutput { var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic; + var $mTouched; # Used for caching function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(), $containsOldMagic = false ) @@ -1984,16 +1985,19 @@ class ParserOutput $this->mLanguageLinks = $languageLinks; $this->mCategoryLinks = $categoryLinks; $this->mContainsOldMagic = $containsOldMagic; + $this->mTouched = ""; } function getText() { return $this->mText; } function getLanguageLinks() { return $this->mLanguageLinks; } function getCategoryLinks() { return $this->mCategoryLinks; } + function getTouched() { return $this->mTouched; } function containsOldMagic() { return $this->mContainsOldMagic; } function setText( $text ) { return wfSetVar( $this->mText, $text ); } function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); } function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); } function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); } + function setTouched( $t ) { return wfSetVar( $this->mTouched, $t ); } function merge( $other ) { $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks ); diff --git a/includes/ParserCache.php b/includes/ParserCache.php index 70b994365c..c682b9039e 100644 --- a/includes/ParserCache.php +++ b/includes/ParserCache.php @@ -2,68 +2,45 @@ class ParserCache { - function get( &$article, &$user ){ + function getKey( &$article, &$user ) { + global $wgDBname; $hash = $user->getPageRenderingHash(); - $pageid = intval( $id ); - $res = wfQuery("SELECT pc_data FROM parsercache WHERE pc_pageid = {$pageid} ". - " AND pc_prefhash = '{$hash}' AND pc_expire > NOW()", DB_WRITE); - $row = wfFetchObject ( $res ); - if( $row ){ - $retVal = unserialize( gzuncompress($row->pc_data) ); - wfProfileOut( $fname ); - } else { - $retVal = false; - } - return $retVal; + $pageid = intval( $article->getID() ); + $key = "$wgDBname:pcache:idhash:$pageid-$hash"; + return $key; } - function save( $parserOutput, &$article, &$user ){ + function get( &$article, &$user ) { + global $wgMemc; $hash = $user->getPageRenderingHash(); $pageid = intval( $article->getID() ); - $title = wfStrencode( $article->mTitle->getPrefixedDBKey() ); - $ser = addslashes( gzcompress( serialize( $parserOutput ) ) ); - if( $parserOutput->containsOldMagic() ){ - $expire = "1 HOUR"; + $key = $this->getKey( $article, $user ); + $value = $wgMemc->get( $key ); + if ( $value ) { + # Delete if article has changed since the cache was made + if ( $value->getTouched() != $article->getTouched() ) { + $wgMemc->delete( $key ); + $value = false; + } } else { - $expire = "7 DAY"; - } - - wfQuery("REPLACE INTO parsercache (pc_prefhash,pc_pageid,pc_title,pc_data, pc_expire) ". - "VALUES('{$hash}', {$pageid}, '{$title}', '{$ser}', ". - "DATE_ADD(NOW(), INTERVAL {$expire}))", DB_WRITE); - - if( rand() % 50 == 0 ){ // more efficient to just do it sometimes - $this->purge(); + $value = false; } + return $value; } - function purge(){ - wfQuery("DELETE FROM parsercache WHERE pc_expire < NOW() LIMIT 250", DB_WRITE); - } - - function clearLinksTo( $pid ){ - $pid = intval( $pid ); - wfQuery("DELETE parsercache FROM parsercache,links ". - "WHERE pc_pageid=links.l_from AND l_to={$pid}", DB_WRITE); - wfQuery("DELETE FROM parsercache WHERE pc_pageid='{$pid}'", DB_WRITE); - } - - # $title is a prefixed db title, for example like Title->getPrefixedDBkey() returns. - function clearBrokenLinksTo( $title ){ - $title = wfStrencode( $title ); - wfQuery("DELETE parsercache FROM parsercache,brokenlinks ". - "WHERE pc_pageid=bl_from AND bl_to='{$title}'", DB_WRITE); - } - - # $pid is a page id - function clearPage( $pid, $namespace ){ - $pid = intval( $pid ); - if( $namespace == NS_MEDIAWIKI ){ - $this->clearLinksTo( $pid ); + function save( $parserOutput, &$article, &$user ){ + global $wgMemc; + $key = $this->getKey( $article, $user ); + $parserOutput->setTouched( $article->getTouched() ); + if( $parserOutput->containsOldMagic() ){ + $expire = 3600; # 1 hour } else { - wfQuery("DELETE FROM parsercache WHERE pc_pageid='{$pid}'", DB_WRITE); + $expire = 7*86400; # 7 days } + + $wgMemc->set( $key, $parserOutput, $expire ); } + } -- 2.20.1