fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-04-20 09:17 am

[dw-free] Memcache domain mapping

[commit: http://hg.dwscoalition.org/dw-free/rev/dceb5bb4ce05]

http://bugs.dwscoalition.org/show_bug.cgi?id=1392

Put the domain mapping into memcache, for efficiency, so we don't have to
hit the database each time.

Patch by exor674, based on modifications by LiveJournal.

Files modified:
  • cgi-bin/Apache/LiveJournal.pm
  • cgi-bin/LJ/Setting/DomainMapping.pm
  • htdocs/manage/domain.bml
--------------------------------------------------------------------------------
diff -r d52d1bafeb4c -r dceb5bb4ce05 cgi-bin/Apache/LiveJournal.pm
--- a/cgi-bin/Apache/LiveJournal.pm	Tue Apr 20 09:12:44 2010 +0000
+++ b/cgi-bin/Apache/LiveJournal.pm	Tue Apr 20 02:22:07 2010 -0700
@@ -874,11 +874,15 @@ sub trans
         my $checkhost = lc($host);
         $checkhost =~ s/^www\.//i;
         $checkhost = $dbr->quote($checkhost);
-        # FIXME: memcache this?
-        my $user = $dbr->selectrow_array(qq{
-            SELECT u.user FROM useridmap u, domains d WHERE
-            u.userid=d.userid AND d.domain=$checkhost
-        });
+        my $key = "domain:$host";
+        my $userid = LJ::MemCache::get($key);
+        unless (defined $userid) {
+            my $db = LJ::get_db_reader();
+            ($userid) = $db->selectrow_array(qq{SELECT userid FROM domains WHERE domain=?}, undef, $host);
+            $userid ||= 0; ## we do cache negative results - if no user for such domain, set userid=0
+            LJ::MemCache::set($key, $userid);
+        }
+        my $user = LJ::load_userid($userid)->user;
         return 404 unless $user;
 
         my $view = $determine_view->($user, "other:$host$hostport", $uri);
diff -r d52d1bafeb4c -r dceb5bb4ce05 cgi-bin/LJ/Setting/DomainMapping.pm
--- a/cgi-bin/LJ/Setting/DomainMapping.pm	Tue Apr 20 09:12:44 2010 +0000
+++ b/cgi-bin/LJ/Setting/DomainMapping.pm	Tue Apr 20 02:22:07 2010 -0700
@@ -43,12 +43,14 @@ sub save {
     $class->errors(domainname => "Can't point to a domain on this site") if $domainname =~ /$LJ::DOMAIN\b/;
 
     # Blank domain = delete mapping
-    if ($domainname eq "") {
-        $dbh->do("DELETE FROM domains WHERE userid=?", undef, $u->{userid});
+    if ( $domainname eq "" ) {
+        $dbh->do( "DELETE FROM domains WHERE userid=?", undef, $u->userid );
+        LJ::MemCache::delete( "domain:" . $u->prop( "journaldomain" ) );
         $u->set_prop("journaldomain", "");
     # If they're able to, change the mapping and update the userprop
-    } elsif ($has_cap) {
+    } elsif ( $has_cap ) {
         return if $domainname eq $u->prop('journaldomain');
+        LJ::MemCache::delete( "domain:" . $u->prop( "journaldomain" ) );
         $dbh->do("INSERT INTO domains VALUES (?, ?)", undef, $domainname, $u->{'userid'});
         if ($dbh->err) {
             my $otherid = $dbh->selectrow_array("SELECT userid FROM domains WHERE domain=?",
@@ -58,8 +60,9 @@ sub save {
                 return;
             }
         }
-        $u->set_prop("journaldomain", $domainname);
-        if ($u->prop('journaldomain')) {
+        $u->set_prop( "journaldomain", $domainname );
+        LJ::MemCache::set( "domain:$domainname", $u->userid );
+        if ( $u->prop( 'journaldomain' ) ) {
             $dbh->do("DELETE FROM domains WHERE userid=? AND domain <> ?",
                      undef, $u->{'userid'}, $domainname);
         }
diff -r d52d1bafeb4c -r dceb5bb4ce05 htdocs/manage/domain.bml
--- a/htdocs/manage/domain.bml	Tue Apr 20 09:12:44 2010 +0000
+++ b/htdocs/manage/domain.bml	Tue Apr 20 02:22:07 2010 -0700
@@ -76,14 +76,17 @@ body<=
             return LJ::bad_input( $ML{'.error.samedomainalias'} ) if $dom =~ /$LJ::DOMAIN\b/;
 
             if ( ( $dom_cap && !$dom ) || ( !$dom_cap && $POST{journaldomain_del} ) ) {
-                $dbh->do( "DELETE FROM domains WHERE userid=?", undef, $u->{userid} );
+                $dbh->do( "DELETE FROM domains WHERE userid=?", undef, $u->userid );
+                LJ::MemCache::delete( "domain:" . $u->prop( "journaldomain" ) );
             } else {
+                LJ::MemCache::delete( "domain:" . $u->prop( "journaldomain" ) );
                 $dbh->do("INSERT INTO domains VALUES (?, ?)", undef, $dom, $u->{'userid'});
                 if ($dbh->err) {
                     my $otherid = $dbh->selectrow_array("SELECT userid FROM domains WHERE domain=?",
                                                         undef, $dom);
                     return LJ::bad_input($ML{'.error.dupdomainalias'}) if $otherid != $u->{'userid'};
                 }
+                LJ::MemCache::set( "domain:" . $dom, $u->userid );
                 if ($u->{'journaldomain'}) {
                     $dbh->do("DELETE FROM domains WHERE userid=? AND domain <> ?",
                              undef, $u->{'userid'}, $dom);
--------------------------------------------------------------------------------