summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <daniel.friesel@uos.de>2020-01-15 18:19:26 +0100
committerDaniel Friesel <daniel.friesel@uos.de>2020-01-15 18:19:26 +0100
commit44a4671500bf7489987e6708199c3a8101381d9d (patch)
tree02aab06537a2e7ab378585fd2157eb834feef34d
parent9cac3de3f8f9b9aa1246b4c63981f4a20cd381e9 (diff)
Cache backend replies by default (if Cache::File is available)
-rw-r--r--Build.PL3
-rwxr-xr-xbin/db-iris42
2 files changed, 39 insertions, 6 deletions
diff --git a/Build.PL b/Build.PL
index 0d83b6d..ce3c1ae 100644
--- a/Build.PL
+++ b/Build.PL
@@ -21,6 +21,9 @@ Module::Build->new(
},
module_name => 'Travel::Status::DE::IRIS',
license => 'perl',
+ recommends => {
+ 'Cache::File' => 0,
+ },
requires => {
'perl' => '5.14.2',
'Carp' => 0,
diff --git a/bin/db-iris b/bin/db-iris
index c0b5a2a..9a05056 100755
--- a/bin/db-iris
+++ b/bin/db-iris
@@ -24,10 +24,12 @@ my $developer_mode = 0;
my $lookahead = 2 * 60;
my $realtime = 0;
my $with_related = 1;
-my ( $filter_via, $track_via, $status_via );
-my ( @grep_class, @grep_type, @grep_platform );
-my ( %edata, @edata_pre );
my $json_output = 0;
+my $use_cache = 1;
+my ( $schedule_cache, $realtime_cache );
+my ( $filter_via, $track_via, $status_via );
+my ( @grep_class, @grep_type, @grep_platform );
+my ( %edata, @edata_pre );
my @output;
@@ -48,6 +50,7 @@ GetOptions(
'v|via=s' => \$filter_via,
'V|track-via=s' => \$track_via,
'x|exact|no-related' => sub { $with_related = 0 },
+ 'cache!' => \$use_cache,
'devmode' => \$developer_mode,
'json' => \$json_output,
'version' => \&show_version,
@@ -134,18 +137,45 @@ for my $efield (@edata_pre) {
}
}
+if ($use_cache) {
+ my $cache_path = $ENV{XDG_CACHE_HOME} // "$ENV{HOME}/.cache";
+ my $schedule_cache_path = "${cache_path}/db-iris-schedule";
+ my $realtime_cache_path = "${cache_path}/db-iris-realtime";
+ eval {
+ use Cache::File;
+ $schedule_cache = Cache::File->new(
+ cache_root => $schedule_cache_path,
+ defaut_expires => '6 hours',
+ lock_level => Cache::File::LOCK_LOCAL(),
+ );
+ $realtime_cache = Cache::File->new(
+ cache_root => $realtime_cache_path,
+ defaut_expires => '180 seconds',
+ lock_level => Cache::File::LOCK_LOCAL(),
+ );
+ };
+ if ($@) {
+ $schedule_cache = undef;
+ $realtime_cache = undef;
+ }
+}
+
my $status = Travel::Status::DE::IRIS->new(
datetime => $datetime,
developer_mode => $developer_mode,
lookahead => $lookahead,
+ main_cache => $schedule_cache,
+ realtime_cache => $realtime_cache,
station => $station,
with_related => $with_related,
);
if ($track_via) {
$status_via = Travel::Status::DE::IRIS->new(
- datetime => $datetime,
- station => $track_via,
- lookahead => $lookahead + 3 * 60,
+ datetime => $datetime,
+ lookahead => $lookahead + 3 * 60,
+ main_cache => $schedule_cache,
+ realtime_cache => $realtime_cache,
+ station => $track_via,
);
}