DBDB: put successful carriage formation requests into long-term cache

This commit is contained in:
Birte Kristina Friesel 2024-08-17 08:55:01 +02:00
parent 60a03632f7
commit cbc1ac791c
No known key found for this signature in database
GPG key ID: 19E6E524EBB177BA

View file

@ -44,18 +44,19 @@ sub has_wagonorder_p {
'https://www.bahn.de/web/api/reisebegleitung/wagenreihung/vehicle-sequence', 'https://www.bahn.de/web/api/reisebegleitung/wagenreihung/vehicle-sequence',
join( '&', map { $_ . '=' . $param{$_} } sort keys %param ) ); join( '&', map { $_ . '=' . $param{$_} } sort keys %param ) );
my $cache = $self->{realtime_cache};
my $promise = Mojo::Promise->new; my $promise = Mojo::Promise->new;
my $debug_prefix my $debug_prefix
= "has_wagonorder_p($opt{train_type} $opt{train_no} @ $opt{eva})"; = "has_wagonorder_p($opt{train_type} $opt{train_no} @ $opt{eva})";
if ( my $content = $cache->get("HEAD $url") ) { if ( my $content = $self->{main_cache}->get("HEAD $url")
// $self->{realtime_cache}->get("HEAD $url") )
{
if ( $content eq 'n' ) { if ( $content eq 'n' ) {
$self->{log}->debug("${debug_prefix}: n (cached)"); $self->{log}->debug("${debug_prefix}: n (cached)");
return $promise->reject; return $promise->reject;
} }
else { else {
$self->{log}->debug("${debug_prefix}: y (cached)"); $self->{log}->debug("${debug_prefix}: ${content} (cached)");
return $promise->resolve($content); return $promise->resolve($content);
} }
} }
@ -66,16 +67,16 @@ sub has_wagonorder_p {
my ($tx) = @_; my ($tx) = @_;
if ( $tx->result->is_success ) { if ( $tx->result->is_success ) {
$self->{log}->debug("${debug_prefix}: a"); $self->{log}->debug("${debug_prefix}: a");
$cache->set( "HEAD $url", 'a' ); $self->{main_cache}->set( "HEAD $url", 'a' );
my $body = decode( 'utf-8', $tx->res->body ); my $body = decode( 'utf-8', $tx->res->body );
my $json = JSON->new->decode($body); my $json = JSON->new->decode($body);
$cache->freeze( $url, $json ); $self->{main_cache}->freeze( $url, $json );
$promise->resolve('a'); $promise->resolve('a');
} }
else { else {
my $code = $tx->res->code; my $code = $tx->res->code;
$self->{log}->debug("${debug_prefix}: n (HTTP $code)"); $self->{log}->debug("${debug_prefix}: n (HTTP $code)");
$cache->set( "HEAD $url", 'n' ); $self->{realtime_cache}->set( "HEAD $url", 'n' );
$promise->reject; $promise->reject;
} }
return; return;
@ -84,7 +85,7 @@ sub has_wagonorder_p {
sub { sub {
my ($err) = @_; my ($err) = @_;
$self->{log}->debug("${debug_prefix}: n ($err)"); $self->{log}->debug("${debug_prefix}: n ($err)");
$cache->set( "HEAD $url", 'n' ); $self->{realtime_cache}->set( "HEAD $url", 'n' );
$promise->reject; $promise->reject;
return; return;
} }
@ -111,10 +112,9 @@ sub get_wagonorder_p {
my $debug_prefix my $debug_prefix
= "get_wagonorder_p($opt{train_type} $opt{train_no} @ $opt{eva})"; = "get_wagonorder_p($opt{train_type} $opt{train_no} @ $opt{eva})";
my $cache = $self->{realtime_cache};
my $promise = Mojo::Promise->new; my $promise = Mojo::Promise->new;
if ( my $content = $cache->thaw($url) ) { if ( my $content = $self->{main_cache}->thaw($url) ) {
$self->{log}->debug("${debug_prefix}: (cached)"); $self->{log}->debug("${debug_prefix}: (cached)");
$promise->resolve($content); $promise->resolve($content);
return $promise; return $promise;
@ -129,7 +129,7 @@ sub get_wagonorder_p {
my $body = decode( 'utf-8', $tx->res->body ); my $body = decode( 'utf-8', $tx->res->body );
my $json = JSON->new->decode($body); my $json = JSON->new->decode($body);
$self->{log}->debug("${debug_prefix}: success"); $self->{log}->debug("${debug_prefix}: success");
$cache->freeze( $url, $json ); $self->{main_cache}->freeze( $url, $json );
$promise->resolve($json); $promise->resolve($json);
} }
else { else {