summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2024-09-28 14:15:14 +0200
committerBirte Kristina Friesel <derf@finalrewind.org>2024-09-28 14:15:14 +0200
commit713715b068143715d02822affd31941592ee9d1e (patch)
tree2634a898d4e01ddab48795709f9dd13c2918e738
parentbaa8c6338e2b464bc9e1c3c12dfe8cc33397b1d9 (diff)
hafas-m: Use cache if Cache::File is available; add --no-cache option
-rwxr-xr-xbin/hafas-m27
1 files changed, 27 insertions, 0 deletions
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<mot> types depend on the used service. Use C<< -m help >> to list them.
Request up to I<count> 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.