Committer: vsukhanov
LJSUP-6730: Fix friends counter in profileU trunk/cgi-bin/LJ/M/FriendsOf.pm U trunk/cgi-bin/LJ/User.pm
Modified: trunk/cgi-bin/LJ/M/FriendsOf.pm =================================================================== --- trunk/cgi-bin/LJ/M/FriendsOf.pm 2010-09-06 07:44:29 UTC (rev 17287) +++ trunk/cgi-bin/LJ/M/FriendsOf.pm 2010-09-06 09:56:01 UTC (rev 17288) @@ -66,7 +66,7 @@ # total edges in, less their mutual friend count if necessary # (which generally includes all communities they're a member of, # as people watch those) - $ct = scalar @{ $fom->_friendof_ids }; + $ct = $fom->{u}->precise_friendsof_count; if ($fom->{mutualsep}) { $ct -= scalar @{ $fom->_mutual_friendids }; } else { Modified: trunk/cgi-bin/LJ/User.pm =================================================================== --- trunk/cgi-bin/LJ/User.pm 2010-09-06 07:44:29 UTC (rev 17287) +++ trunk/cgi-bin/LJ/User.pm 2010-09-06 09:56:01 UTC (rev 17288) @@ -4389,6 +4389,8 @@ return @$uids; } + +## Attention: if 'limit' arg is omited, this method loads all userid from friends table. sub _load_friend_friendof_uids_from_db { my $u = shift; my $mode = shift; @@ -4410,6 +4412,40 @@ return $uids; } +## Returns exact friendsOf count. Whitout limit. +sub precise_friendsof_count { + my $u = shift; + + ## TODO: add caching here + my $ckey = [ $u->userud, "friendof:precise_cnt:" . $u->userid ]; + my $cached = LJ::MemCache::get($ckey); + return $cached if defined $cached; + + ## arrayref with all users friends + my $uids = $u->_load_friend_friendof_uids_from_db('friendofs'); + + my $res = 0; + ## work with batches + while (my @uid_batch = splice @$uids, 0 => 5000){ + my $us = LJ::load_userids(@uid_batch); + foreach my $fuid (@uid_batch){ + my $fu = $us->{$fuid}; + next unless $fu; + my $status = $u->statusvis; + next unless $u->statusvis =~ /^[VML]$/o; + next unless $u->journal_type =~ /^[PI]$/o; + + ## Friend!!! + $res++; + } + } + + LJ::MemCache::set($ckey => $res); + + return $res; +} + + sub fb_push { my $u = shift; eval { @@ -7901,6 +7937,7 @@ # delete friend-of memcache keys for anyone who was added foreach my $fid (@add_ids) { LJ::MemCache::delete([ $userid, "frgmask:$userid:$fid" ]); + LJ::MemCache::delete([ $fid, "friendof:precise_cnt:$fid" ]); LJ::memcache_kill($fid, 'friendofs'); LJ::memcache_kill($fid, 'friendofs2'); @@ -7967,6 +8004,7 @@ # delete friend-of memcache keys for anyone who was removed foreach my $fid (@del_ids) { LJ::MemCache::delete([ $userid, "frgmask:$userid:$fid" ]); + LJ::MemCache::delete([ $fid, "friendof:precise_cnt:$fid" ]); LJ::memcache_kill($fid, 'friendofs'); LJ::memcache_kill($fid, 'friendofs2');