Parser cache moved to memcached
authorTim Starling <tstarling@users.mediawiki.org>
Thu, 27 May 2004 15:24:04 +0000 (15:24 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Thu, 27 May 2004 15:24:04 +0000 (15:24 +0000)
includes/Article.php
includes/Parser.php
includes/ParserCache.php

index 6ccdb7b..710508f 100644 (file)
@@ -1512,6 +1512,10 @@ class Article {
                        return false;
                }
        }
                        return false;
                }
        }
+
+       function getTouched() {
+               return $this->mTouched;
+       }
        
        /* static */ function incViewCount( $id )
        {
        
        /* 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){
 
        # 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();
                
 
                $titles = $title_obj->getBrokenLinksTo();
                
@@ -1585,31 +1589,20 @@ class Article {
                if ( $wgEnablePersistentLC ) {
                        LinkCache::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
                }
                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){
        }
 
        /* static */ function onArticleDelete($title_obj){
-               global $wgEnablePersistentLC, $wgEnableParserCache;
+               global $wgEnablePersistentLC;
                if ( $wgEnablePersistentLC ) {
                        LinkCache::linksccClearLinksTo( $title_obj->getArticleID() );
                }
                if ( $wgEnablePersistentLC ) {
                        LinkCache::linksccClearLinksTo( $title_obj->getArticleID() );
                }
-               if ( $wgEnableParserCache ) {
-                       OutputPage::parsercacheClearLinksTo( $title_obj->getArticleID() );
-               }
        }
 
        /* static */ function onArticleEdit($title_obj){
        }
 
        /* static */ function onArticleEdit($title_obj){
-               global $wgEnablePersistentLC, $wgEnableParserCache;
+               global $wgEnablePersistentLC;
                if ( $wgEnablePersistentLC ) {
                        LinkCache::linksccClearPage( $title_obj->getArticleID() );
                }
                if ( $wgEnablePersistentLC ) {
                        LinkCache::linksccClearPage( $title_obj->getArticleID() );
                }
-               if ( $wgEnableParserCache ) {
-                       OutputPage::parsercacheClearPage( $title_obj->getArticleID(), $title_obj->getNamespace() );
-               }
        }
 }
 
        }
 }
 
index d5bbc15..200c8f4 100644 (file)
@@ -1976,6 +1976,7 @@ class Parser
 class ParserOutput
 {
        var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
 class ParserOutput
 {
        var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
+       var $mTouched; # Used for caching
 
        function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
                $containsOldMagic = false )
 
        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->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 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 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 );
 
        function merge( $other ) {
                $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
index 70b9943..c682b90 100644 (file)
@@ -2,68 +2,45 @@
 
 class ParserCache
 {
 
 class ParserCache
 {
-       function get( &$article, &$user ){
+       function getKey( &$article, &$user ) {
+               global $wgDBname;
                $hash = $user->getPageRenderingHash();
                $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() );
                $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 {
                } 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 {
                } else {
-                       wfQuery("DELETE FROM parsercache WHERE pc_pageid='{$pid}'", DB_WRITE);
+                       $expire = 7*86400; # 7 days
                }
                }
+
+               $wgMemc->set( $key, $parserOutput, $expire );
        }
        }
+       
 }
 
 
 }