From 8aba2fdd8cef5f8d9b0c1a3396463de65ce6545d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20H=C3=A4rter?= Date: Mon, 31 Jul 2023 20:25:06 +0200 Subject: [PATCH] Issue #5: Implemented Cache::FastMmap as caching module. --- F1DataBot.pm | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/F1DataBot.pm b/F1DataBot.pm index de2b1ea..eabdcda 100644 --- a/F1DataBot.pm +++ b/F1DataBot.pm @@ -12,6 +12,7 @@ use Encode; use FindBin; # CPAN packages +use Cache::FastMmap; use JSON; use Log::Log4perl; use YAML; @@ -21,6 +22,12 @@ package F1DataBot; # Constants and initalisations Log::Log4perl->init("$FindBin::Bin/log.conf"); +my $CACHE = Cache::FastMmap->new( + share_file => '/tmp/f1_bot', + serializer => 'json', + unlink_on_exit => 0, + expire_time => 86400, +); sub new { my ( $Type, %Param ) = @_; @@ -186,12 +193,20 @@ sub statistics { my $StatIdentifier = $+{statidentifier}; given ($StatIdentifier) { when ('standings') { - my $Standings = json POST( - join( '/', - ( $Self->{URL}{Ergast}, 'current', 'driverStandings.json' ) - ), - {} - ); + my $Standings; + if ( $CACHE->get('driver_standings') ) { + $Standings = $CACHE->get('driver_standings'); + } + else { + $Standings = json POST( + join( '/', + ( $Self->{URL}{Ergast}, 'current', 'driverStandings.json' ) + ), + {} + ); + $CACHE->set('driver_standings', $Standings); + } + my %DriverStandings; my $DriverStandingsFormatted = sprintf( "%2s %3s %-5s%7s\n", "#", "No.", "Code", "Points" ); @@ -235,16 +250,19 @@ sub statistics { my $StatIdentifier = $+{statidentifier}; given ($StatIdentifier) { when ('standings') { - my $Standings = json POST( - join( - '/', - ( - $Self->{URL}{Ergast}, 'current', - 'constructorStandings.json' - ) - ), - {} - ); + my $Standings; + if ( $CACHE->get('constructor_standings') ) { + $Standings = $CACHE->get('constructor_standings'); + } + else { + $Standings = json POST( + join( '/', + ( $Self->{URL}{Ergast}, 'current', 'constructorStandings.json' ) + ), + {} + ); + $CACHE->set('constructor_standings', $Standings); + } $Self->{LogObject} ->info( 'statistics: Fetched standings are ' . Dumper($Standings) );