Андрей (andy) wrote in changelog,
Андрей
andy
changelog

[ljcom] r11355: LJSUP-10483 (Selfpromo delivery mechanic...

Committer: ailyin
LJSUP-10483 (Selfpromo delivery mechanics is broken)
U   trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm
U   trunk/cgi-bin/LJ/Pay/SelfPromo.pm
Modified: trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm
===================================================================
--- trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm	2012-01-26 13:11:59 UTC (rev 11354)
+++ trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm	2012-01-26 14:04:37 UTC (rev 11355)
@@ -105,8 +105,10 @@
     #
     # note that it also stays valid for admin or poster cancellations because they
     # also only cancel the current promotion
-    my $entry_promoted;
-    if ( $entry_promoted = LJ::Pay::SelfPromo->current_entry ) {
+    my $entry_promoted = LJ::Pay::SelfPromo->current_entry(
+        'require_db' => 1 );
+
+    if ($entry_promoted) {
         LJ::Pay::SelfPromo->debug_msg(
             "cancelling the current entry" . $entry_promoted->url );
 

Modified: trunk/cgi-bin/LJ/Pay/SelfPromo.pm
===================================================================
--- trunk/cgi-bin/LJ/Pay/SelfPromo.pm	2012-01-26 13:11:59 UTC (rev 11354)
+++ trunk/cgi-bin/LJ/Pay/SelfPromo.pm	2012-01-26 14:04:37 UTC (rev 11355)
@@ -31,9 +31,9 @@
 ### HIGH LEVEL INTERFACE ###
 
 sub buyout_cost {
-    my ($class) = @_;
+    my ( $class, %opts ) = @_;
 
-    if ( my $promo = $class->current_entry_info ) {
+    if ( my $promo = $class->current_entry_info( undef, %opts ) ) {
 
         # there actually is a promoted entry right now, so let's
         # return however much its author paid for it plus the bidding step
@@ -68,7 +68,7 @@
     ## community checks
     my $need_promoter_check = 1;
     unless ( $LJ::DISABLED{'community_selfpromo'} ) {
-        my $promo = $class->current_entry_info;
+        my $promo = $class->current_entry_info( undef, 'require_db' => 1 );
         my $entry_active = $promo &&
             LJ::u_equals($promo->promoter, $promoter) &&
             $promo->journalid == $entry->journalid &&
@@ -153,7 +153,7 @@
             $class->raise_error(ERROR_INVALID_PRICE);
         }
 
-        my $min_price = $class->buyout_cost;
+        my $min_price = $class->buyout_cost( 'require_db' => 1 );
         unless ( $price >= $min_price ) {
             LJ::Pay::SelfPromo->debug_msg(
                 "not enough money offered, need at least $min_price");
@@ -186,7 +186,8 @@
         # buyout, and a part of it is a rounding error that
         # goes to a special account made for these purposes
         my ( $refund, $refund_promoid, $refund_userid ) = ( 0, 0, 0 );
-        if ( my $promo = $class->current_entry_info ) {
+        my $promo = $class->current_entry_info( undef, 'require_db' => 1 );
+        if ($promo) {
             $refund         = $class->calculate_refund($promo);
             $refund_promoid = $promo->promoid;
             $refund_userid  = $promo->promoterid || $promo->posterid;
@@ -271,7 +272,7 @@
     my $entry = $args{'entry'};
     my $admin = $args{'admin'};
 
-    my $promo = $class->current_entry_info;
+    my $promo = $class->current_entry_info( undef, 'require_db' => 1 );
     unless ( $promo
         && $promo->journalid == $entry->journalid
         && $promo->jitemid == $entry->jitemid )
@@ -351,7 +352,7 @@
 
     return unless $entry;
 
-    my $promo    = $class->current_entry_info;
+    my $promo    = $class->current_entry_info( undef, 'require_db' => 1 );
     my $journal  = $entry->journal;
     my $poster   = $entry->poster;
 
@@ -409,7 +410,7 @@
 sub withdraw_entry {
     my ( $class, $entry ) = @_;
 
-    my $promo    = $class->current_entry_info;
+    my $promo    = $class->current_entry_info( undef, 'require_db' => 1 );
     my $journal  = $entry->journal;
     my $poster   = $entry->poster;
     my $promoter = $promo->promoter;
@@ -454,9 +455,9 @@
 }
 
 sub current_entry {
-    my ($class) = @_;
+    my ( $class, %opts ) = @_;
 
-    my $promo = $class->current_entry_info;
+    my $promo = $class->current_entry_info( undef, %opts );
     return unless $promo;
     return $promo->entry;
 }
@@ -466,8 +467,8 @@
 
     my $lock = $class->lock;
 
-    my $promo = $class->current_entry_info;
-    my $entry = $class->current_entry;
+    my $promo = $class->current_entry_info( undef, 'require_db' => 1 );
+    my $entry = $class->current_entry( 'require_db' => 1 );
 
     return unless $promo;    # everything's fine if nothing's promoted
     my $poster   = $promo->poster;
@@ -647,28 +648,37 @@
 # $promoid  => $promotion with promoid=$promoid
 # called from LJ::Pay::Payment::PayItem::SelfPromo
 sub current_entry_info {
-    my ( $class, $promoid ) = @_;
+    my ( $class, $promoid, %opts ) = @_;
 
     unless ($promoid) {
-        my $row = LJ::MemCache::get_or_set(
-            $class->memcache_key,
-            sub {
-                my $res = $class->find_promos(
-                    { 'require_active' => 1, 'return_rows' => 1 }
-                );
+        my $from_memcache;
+        unless ( $opts{'require_db'} ) {
+            $from_memcache = LJ::MemCache::get( $class->memcache_key );
+        }
 
-                return 0 unless @$res;
+        # from_memcache = undef means there's nothing in memcache
+        # (or it's disabled in the options), so we need to hit DB;
+        # from_memcache = 0 means 'no current entry';
+        # from_memcache = HASH(0xDEADBEEF) means bless it and return
+        if ( defined $from_memcache ) {
+            return unless $from_memcache;
+            return LJ::Pay::SelfPromo::Promo->new($from_memcache);
+        }
 
-                unless ( @$res == 1 ) {
-                    warn 'more than 1 entry in selfpromo, data corrupt?';
-                }
+        my $rows = $class->find_promos(
+            { 'require_active' => 1, 'return_rows' => 1 } );
 
-                return $res->[0];
-            },
-            3600
-        );
+        unless (@$rows) {
+            LJ::MemCache::set( $class->memcache_key, 0, 30 );
+            return;
+        }
 
-        return unless $row;
+        unless ( @$rows == 1 ) {
+            warn 'more than 1 entry in selfpromo, data corrupt?';
+        }
+
+        my $row = $rows->[0];
+        LJ::MemCache::set( $class->memcache_key, $row, 30 );
         return LJ::Pay::SelfPromo::Promo->new($row);
     }
 
@@ -691,7 +701,7 @@
 
     $class->check_current_entry;
 
-    if ( my $entry_promoted = $class->current_entry ) {
+    if ( my $entry_promoted = $class->current_entry( 'require_db' => 1 ) ) {
 
         # this is an abnormal case, that's why the exception is stringly-typed
 
@@ -727,7 +737,7 @@
 sub deactivate_entry {
     my ( $class, %opts ) = @_;
 
-    my $promo = $class->current_entry_info;
+    my $promo = $class->current_entry_info( undef, 'require_db' => 1 );
     my $entry = $opts{'entry'};
 
     unless ( $promo

Tags: ailyin, andy, ljcom, pm
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