summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2011-08-11 00:50:13 +0200
committerDaniel Friesel <derf@finalrewind.org>2011-08-11 00:50:13 +0200
commit731a744d34b0ce1f670524a1b08d8d71c44288c7 (patch)
tree9b9f8359391ecebbf677afedb776ff7fc9b4cd02
parent143f5a78ab14c8c2c648e69d02845deefe62a399 (diff)
cgi: First try at caching
Uses Cache::File, serializes the ->results because the DE::DeutscheBahn object itself uses XML::LibXML, which fails to serialize.
-rw-r--r--cgi/index.pl24
1 files changed, 22 insertions, 2 deletions
diff --git a/cgi/index.pl b/cgi/index.pl
index 11f69a5..3051f94 100644
--- a/cgi/index.pl
+++ b/cgi/index.pl
@@ -1,9 +1,30 @@
#!/usr/bin/env perl
use Mojolicious::Lite;
+use Cache::File;
use File::ShareDir qw(dist_file);
use HTML::Template;
use Travel::Status::DE::DeutscheBahn;
+sub get_results_for {
+ my ($station) = @_;
+
+ my $cache = Cache::File->new(
+ cache_root => '/tmp/db-fake',
+ default_expires => '900 sec'
+ );
+
+ my $results = $cache->thaw($station);
+
+ if (not $results) {
+ my $status = Travel::Status::DE::DeutscheBahn->new( station => $station
+ );
+ $results = [$status->results];
+ $cache->freeze($station, $results);
+ }
+
+ return @{ $results };
+}
+
get '/' => sub {
my $self = shift;
my $station = $self->param('station');
@@ -18,13 +39,12 @@ get '/multi/:station' => sub {
my $station = $self->stash('station');
my @params;
- my $status = Travel::Status::DE::DeutscheBahn->new( station => $station );
my $template = HTML::Template->new(
filename => dist_file( 'db-fakedisplay', 'multi-lcd.html' ),
loop_context_vars => 1,
);
- for my $result ( $status->results ) {
+ for my $result ( get_results_for($station) ) {
push(
@params,
{