madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r22766: LJSUP-13445: Home page optimization: sta...

Committer: sbelyaev
LJSUP-13445: Home page optimization: stage 2
A   trunk/cgi-bin/LJ/MemCacheProxy.pm
U   trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm
U   trunk/cgi-bin/LJ/User/Rename.pm
U   trunk/cgi-bin/ljlib.pl
U   trunk/cgi-bin/ljprotocol.pl
Added: trunk/cgi-bin/LJ/MemCacheProxy.pm
===================================================================
--- trunk/cgi-bin/LJ/MemCacheProxy.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/MemCacheProxy.pm	2012-08-29 11:29:02 UTC (rev 22766)
@@ -0,0 +1,84 @@
+package LJ::MemCacheProxy;
+
+use strict;
+use warnings;
+
+use LJ::MemCache;
+
+my %singletons = ();
+
+sub reset_singletons {
+    %singletons = ();
+}
+
+sub get {
+    my ($key) = @_;
+
+    $key = $key->[1]
+        if ref $key eq 'ARRAY';
+    
+    if (exists $singletons{$key}) {
+        return $singletons{$key};
+    }
+
+    my $value = LJ::MemCache::get($key);
+    $singletons{$key} = $value;
+    return $value;
+}
+
+sub get_multi {
+    my @keys = @_;
+
+    my $local_ret;
+    my @keys_request = ();
+    foreach my $key (@keys) {
+        if (exists $singletons{$key}) {
+            $local_ret->{$key} = $singletons{$key};
+        } else {
+            push @keys_request, $key;
+        }
+    }
+
+    if (@keys_request) {
+        my $ret = LJ::MemCache::get_multi( @keys_request );
+        while (my ($key, $data) = each %$ret) {
+            $singletons{$key} = $data;
+            $local_ret->{$key} = $data;
+        }
+    }
+
+    return $local_ret;
+}
+
+sub delete {
+    my ($key) = @_;
+
+    $key = $key->[1]
+        if ref $key eq 'ARRAY';
+
+    delete $singletons{$key};
+    LJ::MemCache::delete($key);
+}
+
+sub set {
+    my ($key, $value, $expire) = @_;
+
+    $key = $key->[1]
+        if ref $key eq 'ARRAY';
+
+    $singletons{$key} = $value;
+    return LJ::MemCache::set($key, $value, $expire);
+}
+
+sub replace {
+    my ( $key, $value, $expire ) = @_;
+
+    $key = $key->[1]
+        if ref $key eq 'ARRAY';
+
+    $singletons{$key} = $value;
+    return LJ::MemCache::replace($key, $value, $expire);
+}
+
+1;
+

Modified: trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm
===================================================================
--- trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm	2012-08-29 11:22:51 UTC (rev 22765)
+++ trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm	2012-08-29 11:29:02 UTC (rev 22766)
@@ -1,6 +1,7 @@
 package LJ::RelationService::MysqlAPI;
 use strict;
 
+use LJ::MemCacheProxy;
 
 ## friends
 sub find_relation_destinations {
@@ -200,7 +201,7 @@
     die "create_relation_to error: " . DBI->errstr if DBI->errstr;
 
     my $memkey = [$u->userid, "frgmask:" . $u->userid . ":" . $friend->userid];
-    LJ::MemCache::set($memkey, $opts{groupmask}, time()+60*15);
+    LJ::MemCacheProxy::set($memkey, $opts{groupmask}, time()+60*15);
     LJ::memcache_kill($friend->userid, 'friendofs');
     LJ::memcache_kill($friend->userid, 'friendofs2');
 
@@ -279,7 +280,7 @@
         LJ::User->decrease_friendsof_counter($friend->userid);
 
         # delete friend-of memcache keys for anyone who was removed
-        LJ::MemCache::delete([ $u->userid, "frgmask:" . $u->userid . ":" . $friend->userid ]);
+        LJ::MemCacheProxy::delete([ $u->userid, "frgmask:" . $u->userid . ":" . $friend->userid ]);
         LJ::memcache_kill($friend->userid, 'friendofs');
         LJ::memcache_kill($friend->userid, 'friendofs2');
 
@@ -780,7 +781,7 @@
     return 0 unless $jid && $fid;
 
     my $memkey = [$jid,"frgmask:$jid:$fid"];
-    my $mask = LJ::MemCache::get($memkey);
+    my $mask = LJ::MemCacheProxy::get($memkey);
     unless (defined $mask) {
         my $dbw = LJ::get_db_writer();
         die "No database reader available" unless $dbw;

Modified: trunk/cgi-bin/LJ/User/Rename.pm
===================================================================
--- trunk/cgi-bin/LJ/User/Rename.pm	2012-08-29 11:22:51 UTC (rev 22765)
+++ trunk/cgi-bin/LJ/User/Rename.pm	2012-08-29 11:29:02 UTC (rev 22766)
@@ -3,6 +3,7 @@
 use warnings;
 
 use LJ::Request;
+use LJ::MemCacheProxy;
 use LJ::Event::SecurityAttributeChanged;
 
 ## namespace for user-renaming actions
@@ -239,7 +240,7 @@
                 $dbh->do("DELETE FROM friends WHERE friendid=$u->{'userid'} AND userid IN ($in)");
                 foreach my $fofid ( @$users ) {
                     LJ::memcache_kill( $fofid, "friends" );
-                    LJ::MemCache::delete( [ $fofid, "frgmask:$fofid:$u->{'userid'}" ] );
+                    LJ::MemCacheProxy::delete( [ $fofid, "frgmask:$fofid:$u->{'userid'}" ] );
                 }
             }
         }

Modified: trunk/cgi-bin/ljlib.pl
===================================================================
--- trunk/cgi-bin/ljlib.pl	2012-08-29 11:22:51 UTC (rev 22765)
+++ trunk/cgi-bin/ljlib.pl	2012-08-29 11:29:02 UTC (rev 22766)
@@ -57,6 +57,7 @@
                       LJ::Vertical
                       LJ::Browse
                       LJ::FriendsTags
+                      LJ::MemCacheProxy
                       );
 
 use LJ::TimeUtil;
@@ -2345,6 +2346,7 @@
     %LJ::LOCK_OUT = ();
     %LJ::SECRET = ();                 # secret key -> secret value
 
+
     $LJ::VERTICALS_FORCE_USE_MASTER = 0;    # It need to load a new created category from master insteed slave server.
 
     $LJ::COUNT_LOAD_PROPS_MULTI    = 0;     # Counter for number of requests function LJ::User::load_user_props_multi()
@@ -2357,6 +2359,7 @@
     LJ::Message->reset_singletons;
     LJ::Vertical->reset_singletons;
     LJ::Browse->reset_singletons;
+    LJ::MemCacheProxy->reset_singletons;
 
     LJ::UniqCookie->clear_request_cache;
 

Modified: trunk/cgi-bin/ljprotocol.pl
===================================================================
--- trunk/cgi-bin/ljprotocol.pl	2012-08-29 11:22:51 UTC (rev 22765)
+++ trunk/cgi-bin/ljprotocol.pl	2012-08-29 11:29:02 UTC (rev 22766)
@@ -24,6 +24,7 @@
                       LJ::PersonalStats::Ratings::Journals
                       LJ::API::RateLimiter
                       LJ::Pay::Repost::Offer
+                      LJ::MemCacheProxy
                       );
 
 use LJ::TimeUtil;
@@ -4536,7 +4537,7 @@
             }
 
             my $memkey = [$userid,"frgmask:$userid:$friendid"];
-            LJ::MemCache::set($memkey, $gmask+0, time()+60*15);
+            LJ::MemCacheProxy::set($memkey, $gmask+0, time()+60*15);
             LJ::memcache_kill($friendid, 'friendofs');
             LJ::memcache_kill($friendid, 'friendofs2');
 
@@ -4752,7 +4753,7 @@
         $dbh->do("UPDATE friends SET groupmask=$mask ".
                  "WHERE userid=$userid AND friendid=?",
                  undef, $friendid);
-        LJ::MemCache::set([$userid, "frgmask:$userid:$friendid"], $mask);
+        LJ::MemCacheProxy::set([$userid, "frgmask:$userid:$friendid"], $mask);
     }
 
     # invalidate memcache of friends/groups

Tags: livejournal, madeon, pl, pm, sbelyaev
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments