Loading index.pl +50 −36 Original line number Original line Diff line number Diff line Loading @@ -33,27 +33,36 @@ sub log_api_access { sub get_results_for { sub get_results_for { my ( $backend, $station, %opt ) = @_; my ( $backend, $station, %opt ) = @_; my $data; my $cache = Cache::File->new( my $cache_hafas = Cache::File->new( cache_root => '/tmp/db-fakedisplay', cache_root => '/tmp/dbf-hafas', default_expires => $refresh_interval . ' sec', default_expires => $refresh_interval . ' sec', lock_level => Cache::File::LOCK_LOCAL(), lock_level => Cache::File::LOCK_LOCAL(), ); ); my $cache_iris_main = Cache::File->new( cache_root => '/tmp/dbf-iris-main', default_expires => '2 hours', lock_level => Cache::File::LOCK_LOCAL(), ); my $cache_iris_rt = Cache::File->new( cache_root => '/tmp/dbf-iris-realtime', default_expires => '50 seconds', lock_level => Cache::File::LOCK_LOCAL(), ); # Cache::File has UTF-8 problems, so strip it (and any other potentially # Cache::File has UTF-8 problems, so strip it (and any other potentially # problematic chars). # problematic chars). my $cstation = $station; my $cache_str = $station; $cstation =~ tr{[0-9a-zA-Z -]}{}cd; $cache_str =~ tr{[0-9a-zA-Z -]}{}cd; my $cache_str = "${backend}_${cstation}"; if ( $backend eq 'iris' ) { my $data = $cache->thaw($cache_str); if ( not $data ) { if ( $ENV{DBFAKEDISPLAY_STATS} ) { if ( $ENV{DBFAKEDISPLAY_STATS} ) { log_api_access(); log_api_access(); } } if ( $backend eq 'iris' ) { # requests with DS100 codes should be preferred (they avoid # requests with DS100 codes should be preferred (they avoid # encoding problems on the IRIS server). However, only use them # encoding problems on the IRIS server). However, only use them Loading @@ -66,16 +75,21 @@ sub get_results_for { my $status = Travel::Status::DE::IRIS->new( my $status = Travel::Status::DE::IRIS->new( station => $station, station => $station, serializable => 1, main_cache => $cache_iris_main, realtime_cache => $cache_iris_rt, %opt %opt ); ); $data = { $data = { results => [ $status->results ], results => [ $status->results ], errstr => $status->errstr, errstr => $status->errstr, }; }; $cache->freeze( $cache_str, $data ); } } elsif ( $backend eq 'ris' ) { elsif ( $backend eq 'ris' ) { $data = $cache_hafas->thaw($cache_str); if ( not $data ) { if ( $ENV{DBFAKEDISPLAY_STATS} ) { log_api_access(); } my $status = Travel::Status::DE::HAFAS->new( my $status = Travel::Status::DE::HAFAS->new( station => $station, station => $station, excluded_mots => [qw[bus ferry ondemand tram u]], excluded_mots => [qw[bus ferry ondemand tram u]], Loading @@ -85,7 +99,8 @@ sub get_results_for { results => [ $status->results ], results => [ $status->results ], errstr => $status->errstr, errstr => $status->errstr, }; }; $cache->freeze( $cache_str, $data ); $cache_hafas->freeze( $cache_str, $data ); } } } else { else { $data = { $data = { Loading @@ -93,7 +108,6 @@ sub get_results_for { errstr => "Backend '$backend' not supported", errstr => "Backend '$backend' not supported", }; }; } } } return $data; return $data; } } Loading Loading
index.pl +50 −36 Original line number Original line Diff line number Diff line Loading @@ -33,27 +33,36 @@ sub log_api_access { sub get_results_for { sub get_results_for { my ( $backend, $station, %opt ) = @_; my ( $backend, $station, %opt ) = @_; my $data; my $cache = Cache::File->new( my $cache_hafas = Cache::File->new( cache_root => '/tmp/db-fakedisplay', cache_root => '/tmp/dbf-hafas', default_expires => $refresh_interval . ' sec', default_expires => $refresh_interval . ' sec', lock_level => Cache::File::LOCK_LOCAL(), lock_level => Cache::File::LOCK_LOCAL(), ); ); my $cache_iris_main = Cache::File->new( cache_root => '/tmp/dbf-iris-main', default_expires => '2 hours', lock_level => Cache::File::LOCK_LOCAL(), ); my $cache_iris_rt = Cache::File->new( cache_root => '/tmp/dbf-iris-realtime', default_expires => '50 seconds', lock_level => Cache::File::LOCK_LOCAL(), ); # Cache::File has UTF-8 problems, so strip it (and any other potentially # Cache::File has UTF-8 problems, so strip it (and any other potentially # problematic chars). # problematic chars). my $cstation = $station; my $cache_str = $station; $cstation =~ tr{[0-9a-zA-Z -]}{}cd; $cache_str =~ tr{[0-9a-zA-Z -]}{}cd; my $cache_str = "${backend}_${cstation}"; if ( $backend eq 'iris' ) { my $data = $cache->thaw($cache_str); if ( not $data ) { if ( $ENV{DBFAKEDISPLAY_STATS} ) { if ( $ENV{DBFAKEDISPLAY_STATS} ) { log_api_access(); log_api_access(); } } if ( $backend eq 'iris' ) { # requests with DS100 codes should be preferred (they avoid # requests with DS100 codes should be preferred (they avoid # encoding problems on the IRIS server). However, only use them # encoding problems on the IRIS server). However, only use them Loading @@ -66,16 +75,21 @@ sub get_results_for { my $status = Travel::Status::DE::IRIS->new( my $status = Travel::Status::DE::IRIS->new( station => $station, station => $station, serializable => 1, main_cache => $cache_iris_main, realtime_cache => $cache_iris_rt, %opt %opt ); ); $data = { $data = { results => [ $status->results ], results => [ $status->results ], errstr => $status->errstr, errstr => $status->errstr, }; }; $cache->freeze( $cache_str, $data ); } } elsif ( $backend eq 'ris' ) { elsif ( $backend eq 'ris' ) { $data = $cache_hafas->thaw($cache_str); if ( not $data ) { if ( $ENV{DBFAKEDISPLAY_STATS} ) { log_api_access(); } my $status = Travel::Status::DE::HAFAS->new( my $status = Travel::Status::DE::HAFAS->new( station => $station, station => $station, excluded_mots => [qw[bus ferry ondemand tram u]], excluded_mots => [qw[bus ferry ondemand tram u]], Loading @@ -85,7 +99,8 @@ sub get_results_for { results => [ $status->results ], results => [ $status->results ], errstr => $status->errstr, errstr => $status->errstr, }; }; $cache->freeze( $cache_str, $data ); $cache_hafas->freeze( $cache_str, $data ); } } } else { else { $data = { $data = { Loading @@ -93,7 +108,6 @@ sub get_results_for { errstr => "Backend '$backend' not supported", errstr => "Backend '$backend' not supported", }; }; } } } return $data; return $data; } } Loading