From 2033dcd9d9f73e51476194d6d92c922e30735d4c Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 6 Jun 2015 21:30:44 +0200 Subject: make cache data easier to extend --- index.pl | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'index.pl') diff --git a/index.pl b/index.pl index a13c7d1..c550905 100644 --- a/index.pl +++ b/index.pl @@ -40,9 +40,9 @@ sub get_results { my $sstr = ("${backend} _ ${stop} _ ${city}"); $sstr =~ tr{a-zA-Z0-9}{_}c; - my $results = $cache->thaw($sstr); + my $data = $cache->thaw($sstr); - if ( not $results ) { + if ( not $data ) { my $status; if ( $backend eq 'db' ) { $status = Travel::Status::DE::DeutscheBahn->new( @@ -73,11 +73,14 @@ sub get_results { full_routes => 1, ); } - $results = [ [ $status->results ], $status->errstr ]; - $cache->freeze( $sstr, $results ); + $data = { + results => [ $status->results ], + errstr => $status->errstr + }; + $cache->freeze( $sstr, $data ); } - return @{$results}; + return $data; } sub handle_request { @@ -91,9 +94,8 @@ sub handle_request { my $errstr; if ( ( $city and $stop ) or ( $backend eq 'aseag' and $stop ) ) { - ( undef, $errstr ) - = get_results( $self->param('backend') // $default{backend}, - $city, $stop ); + $errstr = get_results( $self->param('backend') // $default{backend}, + $city, $stop )->{errstr}; } if ( not $no_lines or $no_lines < 1 or $no_lines > 40 ) { @@ -167,10 +169,12 @@ sub get_filtered_departures { my ( @grep_line, @grep_platform, @filtered_results ); - my ( $results, $errstr ) - = get_results( $opt{backend}, $opt{city}, $opt{stop}, + my $data = get_results( $opt{backend}, $opt{city}, $opt{stop}, $opt{cache_expiry} ); + my $results = $data->{results}; + my $errstr = $data->{errstr}; + if ( $opt{filter_line} ) { my @lines = split( qr{,}, $opt{filter_line} ); @grep_line = map { qr{ ^ \Q$_\E }ix } @lines; -- cgit v1.2.3