wisest owl (wisest_owl) wrote in changelog,
wisest owl
wisest_owl
changelog

[livejournal] r17884: LJSUP-7052. Add caching for load_all met...

Committer: gprochaev
LJSUP-7052. Add caching for load_all methods.

U   trunk/cgi-bin/LJ/Browse.pm
U   trunk/cgi-bin/LJ/Vertical.pm
Modified: trunk/cgi-bin/LJ/Browse.pm
===================================================================
--- trunk/cgi-bin/LJ/Browse.pm	2010-12-08 10:04:54 UTC (rev 17883)
+++ trunk/cgi-bin/LJ/Browse.pm	2010-12-08 11:17:31 UTC (rev 17884)
@@ -261,14 +261,24 @@
     my $vert_id = $vertical ? $vertical->vert_id : 0;
     my $where = " WHERE vert_id = $vert_id ";
 
-    my $sth = $dbh->prepare("SELECT * FROM category" . $where);
-    $sth->execute();
-    die $dbh->errstr if $dbh->err;
+    my $cats = LJ::MemCache::get( $class->memkey_catall(vertical => $vertical) );
 
-    my @categories;
-    while (my $row = $sth->fetchrow_hashref) {
-        my $c = $class->new( catid => $row->{catid} );
-        $c->absorb_row($row);
+    unless ($cats && scalar @$cats) {
+        $cats = $dbh->selectall_arrayref(
+                "SELECT * FROM category" . $where,
+                { Slice => {} }
+        );
+        die $dbh->errstr if $dbh->err;
+
+        LJ::MemCache::set( $class->memkey_catall(vertical => $vertical) => $cats );
+    }
+
+    return () unless $cats && scalar @$cats;
+
+    my @categories = ();
+    foreach my $cat (@$cats) {
+        my $c = $class->new( catid => $cat->{catid} );
+        $c->absorb_row($cat);
         $c->set_memcache;
 
         push @categories, $c;
@@ -443,6 +453,16 @@
 # Loaders
 #
 
+sub memkey_catall {
+    my $class = shift;
+    my %args  = @_;
+
+    my $v = $args{'vertical'};
+
+    return [ $v, "catall:".$v->vert_id ] if $v;
+    return "cat:all";
+}
+
 sub memkey_catid {
     my $self = shift;
     my $id = shift;

Modified: trunk/cgi-bin/LJ/Vertical.pm
===================================================================
--- trunk/cgi-bin/LJ/Vertical.pm	2010-12-08 10:04:54 UTC (rev 17883)
+++ trunk/cgi-bin/LJ/Vertical.pm	2010-12-08 11:17:31 UTC (rev 17884)
@@ -509,20 +509,24 @@
     my $dbh = LJ::get_db_writer()
         or die "unable to contact global db master to load vertical";
 
+    my $verticals = LJ::MemCache::get( $class->memkey_vertall );
+    return @$verticals if $verticals && scalar @$verticals;
+
     my $sth = $dbh->prepare("SELECT * FROM vertical2 ORDER BY name");
     $sth->execute;
     die $dbh->errstr if $dbh->err;
 
-    my @verticals;
     while (my $row = $sth->fetchrow_hashref) {
         my $v = $class->new( vert_id => $row->{vert_id} );
         $v->absorb_row($row);
         $v->set_memcache;
 
-        push @verticals, $v;
+        push @$verticals, $v;
     }
 
-    return @verticals;
+    LJ::MemCache::set( $class->memkey_vertall => $verticals );
+
+    return $verticals ? @$verticals : ();
 }
 
 sub load_top_level {
@@ -859,6 +863,12 @@
     return \@result;
 }
 
+sub memkey_vertall {
+    my $class = shift;
+
+    return "vert:all";
+}
+
 sub memkey_vertid {
     my $self = shift;
     my $id = shift;
@@ -908,6 +918,8 @@
 
     LJ::MemCache::delete($self->memkey_vertid);
     LJ::MemCache::delete($self->memkey_vertname);
+    LJ::MemCache::delete($self->memkey_verturl);
+    LJ::MemCache::delete($self->memkey_vertall);
 
     return;
 }
@@ -991,7 +1003,6 @@
 
     my @mem_keys = map { $_->memkey_vertid } @to_load;
     my $memc = LJ::MemCache::get_multi(@mem_keys);
-
     # now which of the objects to load did we get a memcache key for?
     foreach my $obj (@to_load) {
         my $row = $memc->{"vert2:$obj->{vert_id}"};
@@ -1006,24 +1017,26 @@
         or die "unable to contact global db master to load vertical";
 
     my @vals = keys %need;
-    my $bind = LJ::bindstr(@vals);
-    my $sth = $dbh->prepare("SELECT * FROM vertical2 WHERE vert_id IN ($bind)");
-    $sth->execute(@vals);
+    if (scalar @vals) {
+        my $bind = LJ::bindstr(@vals);
+        my $sth = $dbh->prepare("SELECT * FROM vertical2 WHERE vert_id IN ($bind)");
+        $sth->execute(@vals);
 
-    while (my $row = $sth->fetchrow_hashref) {
+        while (my $row = $sth->fetchrow_hashref) {
 
-        # what singleton does this DB row represent?
-        my $obj = $need{$row->{vert_id}};
+            # what singleton does this DB row represent?
+            my $obj = $need{$row->{vert_id}};
 
-        # and update singleton (request cache)
-        $obj->absorb_row($row);
+            # and update singleton (request cache)
+            $obj->absorb_row($row);
 
-        # set in memcache
-        $obj->set_memcache;
+            # set in memcache
+            $obj->set_memcache;
 
-        # and delete from %need for error reporting
-        delete $need{$obj->{vert_id}};
+            # and delete from %need for error reporting
+            delete $need{$obj->{vert_id}};
 
+        }
     }
 
     # weird, vertids that we couldn't find in memcache or db?

Tags: livejournal, pm, wisest-owl
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