[dw-free] better organization of LJ functions
[commit: http://hg.dwscoalition.org/dw-free/rev/9d6b0a0fc48c]
http://bugs.dwscoalition.org/show_bug.cgi?id=3965
Moving LJ::get_timeupdate_multi and LJ::get_times_multi from ljlib.pl to
LJ/User.pm - no package change.
Patch by
kareila.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=3965
Moving LJ::get_timeupdate_multi and LJ::get_times_multi from ljlib.pl to
LJ/User.pm - no package change.
Patch by
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Files modified:
- cgi-bin/LJ/User.pm
- cgi-bin/ljlib.pl
-------------------------------------------------------------------------------- diff -r 70869e7cfca3 -r 9d6b0a0fc48c cgi-bin/LJ/User.pm --- a/cgi-bin/LJ/User.pm Mon Oct 03 13:50:36 2011 +0800 +++ b/cgi-bin/LJ/User.pm Mon Oct 03 13:52:08 2011 +0800 @@ -8068,6 +8068,133 @@ =cut # <LJFUNC> +# name: LJ::get_times_multi +# des: Get the last update time and time create. +# args: opt?, uids +# des-opt: optional hashref, currently can contain 'memcache_only' +# to only retrieve data from memcache +# des-uids: list of userids to load timeupdate and timecreate for +# returns: hashref; uid => {timeupdate => unix timeupdate, timecreate => unix timecreate} +# </LJFUNC> +sub get_times_multi { + my ($opt, @uids) = @_; + + # allow optional opt hashref as first argument + unless (ref $opt eq 'HASH') { + push @uids, $opt; + $opt = {}; + } + return {} unless @uids; + + my @memkeys = map { [$_, "tu:$_"], [$_, "tc:$_"] } @uids; + my $mem = LJ::MemCache::get_multi(@memkeys) || {}; + + my @need = (); + my %times = (); + foreach my $uid (@uids) { + my ($tc, $tu) = ('', ''); + if ($tu = $mem->{"tu:$uid"}) { + $times{updated}->{$uid} = unpack("N", $tu); + } + if ($tc = $mem->{"tc:$_"}){ + $times{created}->{$_} = $tc; + } + + push @need => $uid + unless $tc and $tu; + } + + # if everything was in memcache, return now + return \%times if $opt->{'memcache_only'} or not @need; + + # fill in holes from the database. safe to use the reader because we + # only do an add to memcache, whereas postevent does a set, overwriting + # any potentially old data + my $dbr = LJ::get_db_reader(); + my $need_bind = join(",", map { "?" } @need); + + # Fetch timeupdate and timecreate from DB. + # Timecreate is loaded in pre-emptive goals. + # This is tiny optimization for 'get_timecreate_multi', + # which is called right after this method during + # friends page generation. + my $sth = $dbr->prepare(" + SELECT userid, + UNIX_TIMESTAMP(timeupdate), + UNIX_TIMESTAMP(timecreate) + FROM userusage + WHERE + userid IN ($need_bind)"); + $sth->execute(@need); + while (my ($uid, $tu, $tc) = $sth->fetchrow_array){ + $times{updated}->{$uid} = $tu; + $times{created}->{$uid} = $tc; + + # set memcache for this row + LJ::MemCache::add([$uid, "tu:$uid"], pack("N", $tu), 30*60); + # set this for future use + LJ::MemCache::add([$uid, "tc:$uid"], $tc, 60*60*24); # as in LJ::User->timecreate + } + + return \%times; +} + + +# <LJFUNC> +# name: LJ::get_timeupdate_multi +# des: Get the last time a list of users updated. +# args: opt?, uids +# des-opt: optional hashref, currently can contain 'memcache_only' +# to only retrieve data from memcache +# des-uids: list of userids to load timeupdates for +# returns: hashref; uid => unix timeupdate +# </LJFUNC> +sub get_timeupdate_multi { + my ($opt, @uids) = @_; + + # allow optional opt hashref as first argument + if ( $opt && ref $opt ne 'HASH' ) { + push @uids, $opt; + $opt = {}; + } + return {} unless @uids; + + my @memkeys = map { [$_, "tu:$_"] } @uids; + my $mem = LJ::MemCache::get_multi(@memkeys) || {}; + + my @need; + my %timeupdate; # uid => timeupdate + foreach (@uids) { + if ($mem->{"tu:$_"}) { + $timeupdate{$_} = unpack("N", $mem->{"tu:$_"}); + } else { + push @need, $_; + } + } + + # if everything was in memcache, return now + return \%timeupdate if $opt->{'memcache_only'} || ! @need; + + # fill in holes from the database. safe to use the reader because we + # only do an add to memcache, whereas postevent does a set, overwriting + # any potentially old data + my $dbr = LJ::get_db_reader(); + my $need_bind = join(",", map { "?" } @need); + my $sth = $dbr->prepare("SELECT userid, UNIX_TIMESTAMP(timeupdate) " . + "FROM userusage WHERE userid IN ($need_bind)"); + $sth->execute(@need); + while (my ($uid, $tu) = $sth->fetchrow_array) { + $timeupdate{$uid} = $tu; + + # set memcache for this row + LJ::MemCache::add([$uid, "tu:$uid"], pack("N", $tu), 30*60); + } + + return \%timeupdate; +} + + +# <LJFUNC> # name: LJ::get_timezone # des: Gets the timezone offset for the user. # args: u, offsetref, fakedref diff -r 70869e7cfca3 -r 9d6b0a0fc48c cgi-bin/ljlib.pl --- a/cgi-bin/ljlib.pl Mon Oct 03 13:50:36 2011 +0800 +++ b/cgi-bin/ljlib.pl Mon Oct 03 13:52:08 2011 +0800 @@ -367,133 +367,6 @@ # <LJFUNC> -# name: LJ::get_timeupdate_multi -# des: Get the last time a list of users updated. -# args: opt?, uids -# des-opt: optional hashref, currently can contain 'memcache_only' -# to only retrieve data from memcache -# des-uids: list of userids to load timeupdates for -# returns: hashref; uid => unix timeupdate -# </LJFUNC> -sub get_timeupdate_multi { - my ($opt, @uids) = @_; - - # allow optional opt hashref as first argument - if ( $opt && ref $opt ne 'HASH' ) { - push @uids, $opt; - $opt = {}; - } - return {} unless @uids; - - my @memkeys = map { [$_, "tu:$_"] } @uids; - my $mem = LJ::MemCache::get_multi(@memkeys) || {}; - - my @need; - my %timeupdate; # uid => timeupdate - foreach (@uids) { - if ($mem->{"tu:$_"}) { - $timeupdate{$_} = unpack("N", $mem->{"tu:$_"}); - } else { - push @need, $_; - } - } - - # if everything was in memcache, return now - return \%timeupdate if $opt->{'memcache_only'} || ! @need; - - # fill in holes from the database. safe to use the reader because we - # only do an add to memcache, whereas postevent does a set, overwriting - # any potentially old data - my $dbr = LJ::get_db_reader(); - my $need_bind = join(",", map { "?" } @need); - my $sth = $dbr->prepare("SELECT userid, UNIX_TIMESTAMP(timeupdate) " . - "FROM userusage WHERE userid IN ($need_bind)"); - $sth->execute(@need); - while (my ($uid, $tu) = $sth->fetchrow_array) { - $timeupdate{$uid} = $tu; - - # set memcache for this row - LJ::MemCache::add([$uid, "tu:$uid"], pack("N", $tu), 30*60); - } - - return \%timeupdate; -} - - -# <LJFUNC> -# name: LJ::get_times_multi -# des: Get the last update time and time create. -# args: opt?, uids -# des-opt: optional hashref, currently can contain 'memcache_only' -# to only retrieve data from memcache -# des-uids: list of userids to load timeupdate and timecreate for -# returns: hashref; uid => {timeupdate => unix timeupdate, timecreate => unix timecreate} -# </LJFUNC> -sub get_times_multi { - my ($opt, @uids) = @_; - - # allow optional opt hashref as first argument - unless (ref $opt eq 'HASH') { - push @uids, $opt; - $opt = {}; - } - return {} unless @uids; - - my @memkeys = map { [$_, "tu:$_"], [$_, "tc:$_"] } @uids; - my $mem = LJ::MemCache::get_multi(@memkeys) || {}; - - my @need = (); - my %times = (); - foreach my $uid (@uids) { - my ($tc, $tu) = ('', ''); - if ($tu = $mem->{"tu:$uid"}) { - $times{updated}->{$uid} = unpack("N", $tu); - } - if ($tc = $mem->{"tc:$_"}){ - $times{created}->{$_} = $tc; - } - - push @need => $uid - unless $tc and $tu; - } - - # if everything was in memcache, return now - return \%times if $opt->{'memcache_only'} or not @need; - - # fill in holes from the database. safe to use the reader because we - # only do an add to memcache, whereas postevent does a set, overwriting - # any potentially old data - my $dbr = LJ::get_db_reader(); - my $need_bind = join(",", map { "?" } @need); - - # Fetch timeupdate and timecreate from DB. - # Timecreate is loaded in pre-emptive goals. - # This is tiny optimization for 'get_timecreate_multi', - # which is called right after this method during - # friends page generation. - my $sth = $dbr->prepare(" - SELECT userid, - UNIX_TIMESTAMP(timeupdate), - UNIX_TIMESTAMP(timecreate) - FROM userusage - WHERE - userid IN ($need_bind)"); - $sth->execute(@need); - while (my ($uid, $tu, $tc) = $sth->fetchrow_array){ - $times{updated}->{$uid} = $tu; - $times{created}->{$uid} = $tc; - - # set memcache for this row - LJ::MemCache::add([$uid, "tu:$uid"], pack("N", $tu), 30*60); - # set this for future use - LJ::MemCache::add([$uid, "tc:$uid"], $tc, 60*60*24); # as in LJ::User->timecreate - } - - return \%times; -} - - -# <LJFUNC> # name: LJ::register_authaction # des: Registers a secret to have the user validate. # info: Some things, like requiring a user to validate their e-mail address, require --------------------------------------------------------------------------------