From 713715b068143715d02822affd31941592ee9d1e Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Sat, 28 Sep 2024 14:15:14 +0200 Subject: hafas-m: Use cache if Cache::File is available; add --no-cache option --- bin/hafas-m | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'bin') diff --git a/bin/hafas-m b/bin/hafas-m index 844040e..357cc70 100755 --- a/bin/hafas-m +++ b/bin/hafas-m @@ -21,6 +21,8 @@ my $num_results = 30; my $types = q{}; my $developer_mode; my $via; +my $use_cache = 1; +my $cache; my ( $json_output, $raw_json_output ); my ( $list_services, $service ); my ( @excluded_mots, @exclusive_mots ); @@ -47,6 +49,7 @@ GetOptions( 't|time=s' => \$time, 'v|via=s' => \$via, 'V|version' => \&show_version, + 'cache!' => \$use_cache, 'devmode' => \$developer_mode, 'json' => \$json_output, 'raw-json' => \$raw_json_output, @@ -71,9 +74,26 @@ if ($list_services) { exit 0; } +if ($use_cache) { + my $cache_path = ( $ENV{XDG_CACHE_HOME} // "$ENV{HOME}/.cache" ) + . '/Travel-Status-DE-HAFAS'; + eval { + require Cache::File; + $cache = Cache::File->new( + cache_root => $cache_path, + default_expires => '90 seconds', + lock_level => Cache::File::LOCK_LOCAL(), + ); + }; + if ($@) { + $cache = undef; + } +} + parse_mot_options(); my %opt = ( + cache => $cache, excluded_mots => \@excluded_mots, exclusive_mots => \@exclusive_mots, station => shift || show_help(1), @@ -804,6 +824,13 @@ The I types depend on the used service. Use C<< -m help >> to list them. Request up to I results. Default: 30. +=item B<--no-cache> + +If the Cache::File module is available, server replies are cached in +F<~/.cache/Travel-Status-DE-HAFAS> (or a path relative to C<$XDG_CACHE_HOME>, +if set) for 90 seconds. Use this option to disable caching. You can also use +B<--cache> to re-enable it. + =item B<--raw-json> Print unprocessed HAFAS response as JSON and exit. -- cgit v1.2.3