diff --git a/app.pl b/app.pl index 79f3404..fdb395f 100755 --- a/app.pl +++ b/app.pl @@ -20,6 +20,7 @@ use constant MIN_CUTOFF => 0; use constant MAX_CUTOFF => 20; use constant RATE_LIMIT_N => 20; use constant RATE_LIMIT_WINDOW => 600; +use constant CACHE_TTL => 900; use constant RECENT_COOKIE => 'recent'; use constant RECENT_MAX => 10; @@ -81,12 +82,13 @@ sub check_rate_limit { } sub cache_lookup { - my ($code, $cutoff) = @_; + my ($code, $cutoff, %opts) = @_; my $row = $dbh->selectrow_hashref( 'SELECT * FROM cache WHERE report_code = ? AND wipe_cutoff = ?', undef, $code, $cutoff ); return undef unless $row; + return undef if !$opts{ignore_ttl} && time() - $row->{fetched_at} > CACHE_TTL; return { title => $row->{title}, raid_date => $row->{raid_date}, @@ -100,7 +102,10 @@ sub cache_store { my ($code, $cutoff, $result) = @_; $dbh->do( 'INSERT INTO cache (report_code, wipe_cutoff, title, raid_date, zone_name, fight_count, deaths_json, fetched_at) - VALUES (?,?,?,?,?,?,?,?)', + VALUES (?,?,?,?,?,?,?,?) + ON CONFLICT(report_code, wipe_cutoff) DO UPDATE SET + title = excluded.title, raid_date = excluded.raid_date, zone_name = excluded.zone_name, + fight_count = excluded.fight_count, deaths_json = excluded.deaths_json, fetched_at = excluded.fetched_at', undef, $code, $cutoff, $result->{title}, $result->{raid_date}, $result->{zone_name}, $result->{fight_count}, encode_json($result->{deaths}), time() ); @@ -150,7 +155,7 @@ sub recent_entries { my @out; for my $p (@$pairs) { my ($code, $cutoff) = @$p; - my $cached = cache_lookup($code, $cutoff); + my $cached = cache_lookup($code, $cutoff, ignore_ttl => 1); next unless $cached; push @out, { code => $code, cutoff => $cutoff,