summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2015-10-22 12:49:49 +0200
committerDaniel Friesel <derf@finalrewind.org>2015-10-22 12:49:49 +0200
commit72fe1983fd85a946f37b3c52ccba813f39ba5b16 (patch)
treee80be1ddc8ec28f6390ae311e739eddbe168de54
parent8957db765d0ffa076ce07061a3e16b7f4ebb81a4 (diff)
cache data for landing page and forms
-rw-r--r--index.pl31
1 files changed, 29 insertions, 2 deletions
diff --git a/index.pl b/index.pl
index 7a6d018..7bb6b9a 100644
--- a/index.pl
+++ b/index.pl
@@ -1,5 +1,6 @@
#!/usr/bin/env perl
use Mojolicious::Lite;
+use Cache::File;
use DBI;
use Encode qw(decode encode);
use Travel::Status::DE::IRIS;
@@ -97,7 +98,18 @@ helper barplot_filters => sub {
my ($self) = @_;
my $dbh = $self->app->dbh;
- my $ret = {
+ my $cache = Cache::File->new(
+ cache_root => '/tmp/dbdb',
+ default_expires => '12 hours',
+ lock_level => Cache::File::LOCK_LOCAL(),
+ );
+
+ my $ret = $cache->thaw('barplot_filters');
+ if ($ret) {
+ return $ret;
+ }
+
+ $ret = {
lines => [
q{},
map { [ $_->[2] . ' ' . $_->[3], $_->[0] . '.' . $_->[1] ] } @{
@@ -151,6 +163,8 @@ helper barplot_filters => sub {
],
};
+ $cache->freeze( 'barplot_filters', $ret );
+
return $ret;
};
@@ -178,6 +192,17 @@ helper globalstats => sub {
my ($self) = @_;
my $dbh = $self->app->dbh;
+ my $cache = Cache::File->new(
+ cache_root => '/tmp/dbdb',
+ default_expires => '12 hours',
+ lock_level => Cache::File::LOCK_LOCAL(),
+ );
+
+ my $ret = $cache->thaw('globalstats');
+ if ($ret) {
+ return $ret;
+ }
+
my $stations = [
map { Travel::Status::DE::IRIS::Stations::get_station($_)->[1] } @{
$self->app->dbh->selectcol_arrayref(
@@ -185,7 +210,7 @@ helper globalstats => sub {
}
];
- my $ret = {
+ $ret = {
departures => $self->count_unique_column(),
stationlist => $stations,
stations => $self->count_unique_column('station'),
@@ -215,6 +240,8 @@ helper globalstats => sub {
"select avg(delay) from departures where not is_canceled"),
};
+ $cache->freeze( 'globalstats', $ret );
+
return $ret;
};