From 731a744d34b0ce1f670524a1b08d8d71c44288c7 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 11 Aug 2011 00:50:13 +0200 Subject: cgi: First try at caching Uses Cache::File, serializes the ->results because the DE::DeutscheBahn object itself uses XML::LibXML, which fails to serialize. --- cgi/index.pl | 24 ++++++++++++++++++++++-- 1 file 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, { -- cgit v1.2.3