From: Steinar H. Gunderson Date: Fri, 10 Nov 2023 16:36:36 +0000 (+0100) Subject: Cache /properties; it was getting untenably slow for something that nearly never... X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=ec45bdd68a37a3dd8a824fda7e88c5e43b52ac9c;p=skvidarsync Cache /properties; it was getting untenably slow for something that nearly never changes. --- diff --git a/bin/sync.pl b/bin/sync.pl index 9e00dc4..e913c16 100644 --- a/bin/sync.pl +++ b/bin/sync.pl @@ -251,7 +251,7 @@ sub update_assignment_db { } sub get_spreadsheet_with_title { - my ($ua, $token, $wanted_sheet_title) = @_; + my ($dbh, $ua, $token, $invitation_ts, $wanted_sheet_title) = @_; # See if we have any spreadsheets that match this title. my $start = [Time::HiRes::gettimeofday]; @@ -267,6 +267,8 @@ sub get_spreadsheet_with_title { my $sheet_id = $sheet->{'properties'}{'sheetId'}; if ($title =~ /\Q$wanted_sheet_title\E/) { # skv_log("Synkroniserer ($config::invitation_channel, $invitation_ts) mot arket “$title” (fane-ID $sheet_id)."); + $dbh->do('UPDATE message_sheet_link SET tab_name=?, tab_id=? WHERE channel=? AND ts=?', + undef, $title, $sheet_id, $config::invitation_channel, $invitation_ts); return ($title, $sheet_id); } } @@ -495,17 +497,21 @@ sub run { my $invitation_ts = $linkref->{'ts'}; my $wanted_sheet_title = $linkref->{'sheet_title'}; die "Could not get newest sheet title" if (!defined($wanted_sheet_title)); - - my ($tab_name, $tab_id) = get_spreadsheet_with_title($ua, $token, $wanted_sheet_title); - if (!defined($tab_name)) { - skv_log("Fant ikke noen fane med «$wanted_sheet_title» i navnet; kan ikke synkronisere.\n"); - sheet_batch_update($ua, $token, [ serialize_skv_log_to_sheet() ]); - die; - } + my $tab_name = $linkref->{'tab_name'}; + my $tab_id = $linkref->{'tab_id'}; # Store away the second-newest ID. my $prev_invitation_ts = $q->fetchrow_hashref->{'ts'}; + if (!defined($tab_name) || !defined($tab_id)) { + ($tab_name, $tab_id) = get_spreadsheet_with_title($dbh, $ua, $token, $invitation_ts, $wanted_sheet_title); + if (!defined($tab_name)) { + skv_log("Fant ikke noen fane med «$wanted_sheet_title» i navnet; kan ikke synkronisere.\n"); + sheet_batch_update($ua, $token, [ serialize_skv_log_to_sheet() ]); + die; + } + } + # Find everyone who are marked as attending on Slack (via reactions). $q = $dbh->prepare('SELECT DISTINCT userid,reaction FROM current_reactions WHERE channel=? AND ts=? AND reaction IN (\'heart\', \'open_mouth\', \'blue_heart\')'); $q->execute($config::invitation_channel, $invitation_ts); diff --git a/bin/ws.pl b/bin/ws.pl index fd01cb5..79394f3 100644 --- a/bin/ws.pl +++ b/bin/ws.pl @@ -119,6 +119,8 @@ sub handle_event { print STDERR "Matching message {$channel, $ts} to date $date\n"; $dbh->do('INSERT INTO message_sheet_link (channel, ts, sheet_title) VALUES (?,?,?)', undef, $channel, $ts, $date); + # Blow the cache. + $dbh->do('UPDATE message_sheet_link SET tab_name=NULL, tab_id=NULL WHERE channel=?', undef, $channel); } else { print STDERR "No date found in message, ignoring\n"; } diff --git a/skvidarlang.sql b/skvidarlang.sql index 5f0db3b..020d066 100644 --- a/skvidarlang.sql +++ b/skvidarlang.sql @@ -20,6 +20,8 @@ CREATE TABLE message_sheet_link ( channel VARCHAR NOT NULL, ts VARCHAR NOT NULL, sheet_title VARCHAR NOT NULL, + tab_name VARCHAR, -- Cache; blown away every time we see a new message. + tab_id VARCHAR, -- Ditto. PRIMARY KEY (channel, ts) );