From 1fb2c0b50e098207b484022f21643428555bbc1a Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 7 Jun 2019 16:31:30 +0200 Subject: use global cache objects, also cache wagon order --- lib/DBInfoscreen.pm | 37 +++++++++++++++++ lib/DBInfoscreen/Controller/Stationboard.pm | 62 +++++++++-------------------- lib/DBInfoscreen/Controller/Wagenreihung.pm | 1 + 3 files changed, 56 insertions(+), 44 deletions(-) diff --git a/lib/DBInfoscreen.pm b/lib/DBInfoscreen.pm index 548fce2..d83f58f 100644 --- a/lib/DBInfoscreen.pm +++ b/lib/DBInfoscreen.pm @@ -4,6 +4,7 @@ use Mojo::Base 'Mojolicious'; # Copyright (C) 2011-2019 Daniel Friesel # License: 2-Clause BSD +use Cache::File; use Travel::Status::DE::HAFAS; use Travel::Status::DE::HAFAS::StopFinder; use Travel::Status::DE::IRIS::Stations; @@ -23,6 +24,42 @@ my %default = ( sub startup { my ($self) = @_; + $self->attr( + cache_hafas => sub { + my ($self) = @_; + return Cache::File->new( + cache_root => $ENV{DBFAKEDISPLAY_HAFAS_CACHE} + // '/tmp/dbf-hafas', + default_expires => '180 seconds', + lock_level => Cache::File::LOCK_LOCAL(), + ); + } + ); + + $self->attr( + cache_iris_main => sub { + my ($self) = @_; + return Cache::File->new( + cache_root => $ENV{DBFAKEDISPLAY_IRIS_CACHE} + // '/tmp/dbf-iris-main', + default_expires => '6 hours', + lock_level => Cache::File::LOCK_LOCAL(), + ); + } + ); + + $self->attr( + cache_iris_rt => sub { + my ($self) = @_; + return Cache::File->new( + cache_root => $ENV{DBFAKEDISPLAY_IRISRT_CACHE} + // '/tmp/dbf-iris-realtime', + default_expires => '70 seconds', + lock_level => Cache::File::LOCK_LOCAL(), + ); + } + ); + $self->helper( 'handle_no_results' => sub { my ( $self, $backend, $station, $errstr ) = @_; diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm index 7657a85..449d340 100644 --- a/lib/DBInfoscreen/Controller/Stationboard.pm +++ b/lib/DBInfoscreen/Controller/Stationboard.pm @@ -4,7 +4,6 @@ use Mojo::Base 'Mojolicious::Controller'; # Copyright (C) 2011-2019 Daniel Friesel # License: 2-Clause BSD -use Cache::File; use DateTime; use DateTime::Format::Strptime; use Encode qw(decode encode); @@ -195,20 +194,7 @@ sub hafas_xml_req { # quick&dirty, will be cleaned up later sub get_route_timestamps { - my ( $ua, $train ) = @_; - - my $cache_iris_main = Cache::File->new( - cache_root => $ENV{DBFAKEDISPLAY_IRIS_CACHE} // '/tmp/dbf-iris-main', - default_expires => '6 hours', - lock_level => Cache::File::LOCK_LOCAL(), - ); - - my $cache_iris_rt = Cache::File->new( - cache_root => $ENV{DBFAKEDISPLAY_IRISRT_CACHE} - // '/tmp/dbf-iris-realtime', - default_expires => '70 seconds', - lock_level => Cache::File::LOCK_LOCAL(), - ); + my ( $ua, $cache_main, $cache_rt, $train ) = @_; $ua->request_timeout(3); @@ -218,7 +204,7 @@ sub get_route_timestamps { my $date_yyyy = $train->start->strftime('%d.%m.%Y'); my $train_no = $train->type . ' ' . $train->train_no; - my $trainsearch = hafas_json_req( $ua, $cache_iris_main, + my $trainsearch = hafas_json_req( $ua, $cache_main, "${base}&date=${date_yy}&trainname=${train_no}" ); if ( not $trainsearch ) { @@ -246,14 +232,14 @@ sub get_route_timestamps { $base = 'https://reiseauskunft.bahn.de/bin/traininfo.exe/dn'; - my $traininfo = hafas_json_req( $ua, $cache_iris_main, + my $traininfo = hafas_json_req( $ua, $cache_main, "${base}/${trainlink}?rt=1&date=${date_yy}&L=vs_json" ); if ( not $traininfo or $traininfo->{error} ) { return; } - my $traindelay = hafas_xml_req( $ua, $cache_iris_rt, + my $traindelay = hafas_xml_req( $ua, $cache_rt, "${base}/${trainlink}?rt=1&date=${date_yy}&L=vs_java3" ); my $ret = {}; @@ -297,25 +283,6 @@ sub get_results_for { my ( $backend, $station, %opt ) = @_; my $data; - my $cache_hafas = Cache::File->new( - cache_root => $ENV{DBFAKEDISPLAY_HAFAS_CACHE} // '/tmp/dbf-hafas', - default_expires => '180 seconds', - lock_level => Cache::File::LOCK_LOCAL(), - ); - - my $cache_iris_main = Cache::File->new( - cache_root => $ENV{DBFAKEDISPLAY_IRIS_CACHE} // '/tmp/dbf-iris-main', - default_expires => '6 hours', - lock_level => Cache::File::LOCK_LOCAL(), - ); - - my $cache_iris_rt = Cache::File->new( - cache_root => $ENV{DBFAKEDISPLAY_IRISRT_CACHE} - // '/tmp/dbf-iris-realtime', - default_expires => '70 seconds', - lock_level => Cache::File::LOCK_LOCAL(), - ); - # Cache::File has UTF-8 problems, so strip it (and any other potentially # problematic chars). my $cache_str = $station; @@ -336,8 +303,8 @@ sub get_results_for { $station = $station_matches[0][0]; my $status = Travel::Status::DE::IRIS->new( station => $station, - main_cache => $cache_iris_main, - realtime_cache => $cache_iris_rt, + main_cache => $opt{cache_iris_main}, + realtime_cache => $opt{cache_iris_rt}, log_dir => $ENV{DBFAKEDISPLAY_XMLDUMP_DIR}, lookbehind => 20, lwp_options => { @@ -367,7 +334,7 @@ sub get_results_for { } } elsif ( $backend eq 'ris' ) { - $data = $cache_hafas->thaw($cache_str); + $data = $opt{cache_hafas}->thaw($cache_str); if ( not $data ) { if ( $ENV{DBFAKEDISPLAY_STATS} ) { log_api_access(); @@ -385,7 +352,7 @@ sub get_results_for { results => [ $status->results ], errstr => $status->errstr, }; - $cache_hafas->freeze( $cache_str, $data ); + $opt{cache_hafas}->freeze( $cache_str, $data ); } } else { @@ -419,7 +386,11 @@ sub handle_request { my $save_defaults = $self->param('save_defaults') // 0; my $limit = $self->param('limit') // 0; my @train_types = split( /,/, $self->param('train_types') // q{} ); - my %opt; + my %opt = ( + cache_hafas => $self->app->cache_hafas, + cache_iris_main => $self->app->cache_iris_main, + cache_iris_rt => $self->app->cache_iris_rt, + ); my $api_version = $backend eq 'iris' @@ -943,8 +914,11 @@ sub handle_request { [ $result->sched_route_post ] ) ]; - my ( $route_ts, $him ) - = get_route_timestamps( $self->ua, $result ); + my ( $route_ts, $him ) = get_route_timestamps( + $self->ua, + $self->app->cache_iris_main, + $self->app->cache_iris_rt, $result + ); if ($route_ts) { for my $elem ( @{ $departures[-1]{route_pre_diff} }, diff --git a/lib/DBInfoscreen/Controller/Wagenreihung.pm b/lib/DBInfoscreen/Controller/Wagenreihung.pm index 986019b..fa501a8 100644 --- a/lib/DBInfoscreen/Controller/Wagenreihung.pm +++ b/lib/DBInfoscreen/Controller/Wagenreihung.pm @@ -12,6 +12,7 @@ sub wagenreihung { my $departure = $self->stash('departure'); my $wr = Travel::Status::DE::DBWagenreihung->new( + cache => $self->app->cache_iris_rt, departure => $departure, train_number => $train, ); -- cgit v1.2.3