[dw-free] DW::Pay::get_current_account_status doesn't cache results
[commit: http://hg.dwscoalition.org/dw-free/rev/b7a2c77e9360]
http://bugs.dwscoalition.org/show_bug.cgi?id=3246
Cache the current account status for 15 minutes, so we don't hit the
database so often.
Patch by
kareila.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=3246
Cache the current account status for 15 minutes, so we don't hit the
database so often.
Patch by
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Files modified:
- cgi-bin/DW/Pay.pm
- doc/raw/memcache-keys.txt
-------------------------------------------------------------------------------- diff -r 5b5ddc428e5d -r b7a2c77e9360 cgi-bin/DW/Pay.pm --- a/cgi-bin/DW/Pay.pm Wed Aug 03 11:15:32 2011 +0800 +++ b/cgi-bin/DW/Pay.pm Wed Aug 03 11:35:53 2011 +0800 @@ -177,17 +177,48 @@ # # uuserid required user object or userid to get paid status of # -# RETURN: undef for free, else a typeid of the account type. +# RETURN: typeid from get_paid_status, or else default_typeid # sub get_current_account_status { - # try to get current paid status + my $uid = LJ::want_userid( $_[0] ); + return DW::Pay::default_typeid() unless $uid; + + # check memcache first + my $memkey = [ $uid, "accttype:$uid" ]; + my $typeid = LJ::MemCache::get( $memkey ); + return $typeid if defined $typeid; + + # try to get current paid status if not in memcache my $stat = DW::Pay::get_paid_status( @_ ); # default check - return DW::Pay::default_typeid() if DW::Pay::is_default_type( $stat ); + $typeid = DW::Pay::is_default_type( $stat ) + ? DW::Pay::default_typeid() : $stat->{typeid}; - # valid row, return whatever type it is - return $stat->{typeid}; + # store in memcache for 15 minutes + LJ::MemCache::set( $memkey, $typeid, 900 ); + + return $typeid; +} + +################################################################################ +# DW::Pay::expire_status_cache +# +# ARGUMENTS: uuserid +# +# uuserid required user object or userid to expire status cache of +# +# RETURN: undef on error, else 1 on success. +# +sub expire_status_cache { + my $uid = LJ::want_userid( $_[0] ); + return undef unless $uid; + + my $memkey = [ $uid, "accttype:$uid" ]; + LJ::MemCache::delete( $memkey ); + delete $LJ::PAID_STATUS{$uid}; + + return 1; } ################################################################################ @@ -488,7 +519,7 @@ or return error( ERR_FATAL, "Invalid/not a user object." ); my %cols = ( @_ ) or return error( ERR_FATAL, "Nothing to change!" ); - delete $LJ::PAID_STATUS{$u->id}; + DW::Pay::expire_status_cache( $u->id ); my $dbh = DW::Pay::get_db_writer() or return error( ERR_TEMP, "Unable to get db writer." ); @@ -602,7 +633,7 @@ $dbh->do( q{UPDATE dw_paidstatus SET expiretime=? WHERE userid=?}, undef, $row->{datetime}, $u->id ); return error( ERR_FATAL, "Database error: " . $dbh->errstr ) if $dbh->err; - delete $LJ::PAID_STATUS{$u->id}; + DW::Pay::expire_status_cache( $u->id ); return 1; } diff -r 5b5ddc428e5d -r b7a2c77e9360 doc/raw/memcache-keys.txt --- a/doc/raw/memcache-keys.txt Wed Aug 03 11:15:32 2011 +0800 +++ b/doc/raw/memcache-keys.txt Wed Aug 03 11:35:53 2011 +0800 @@ -5,6 +5,7 @@ <uid> sess:<uid>:<sessid> == sessions row hashref <uid> bio:<uid> == user bio text <uid> kws:<uid> == { kwid => keyword }; hashref of keyword ids and keywords +<uid> accttype:<uid> == %LJ::CAP key for user's account, 15 min <uid> talkprop:<uid>:<jtalkid> == { propname => $value, ... } <uid> talksubject:<cid>:<uid>:<jtalkid> == scalar --------------------------------------------------------------------------------