Issue #5: Implemented Cache::FastMmap as caching module. #8

Merged
demiguise merged 1 commits from issue-5-caching into master 2023-07-31 20:26:39 +02:00

View File

@ -12,6 +12,7 @@ use Encode;
use FindBin; use FindBin;
# CPAN packages # CPAN packages
use Cache::FastMmap;
use JSON; use JSON;
use Log::Log4perl; use Log::Log4perl;
use YAML; use YAML;
@ -21,6 +22,12 @@ package F1DataBot;
# Constants and initalisations # Constants and initalisations
Log::Log4perl->init("$FindBin::Bin/log.conf"); 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 { sub new {
my ( $Type, %Param ) = @_; my ( $Type, %Param ) = @_;
@ -186,12 +193,20 @@ sub statistics {
my $StatIdentifier = $+{statidentifier}; my $StatIdentifier = $+{statidentifier};
given ($StatIdentifier) { given ($StatIdentifier) {
when ('standings') { when ('standings') {
my $Standings = json POST( my $Standings;
if ( $CACHE->get('driver_standings') ) {
$Standings = $CACHE->get('driver_standings');
}
else {
$Standings = json POST(
join( '/', join( '/',
( $Self->{URL}{Ergast}, 'current', 'driverStandings.json' ) ( $Self->{URL}{Ergast}, 'current', 'driverStandings.json' )
), ),
{} {}
); );
$CACHE->set('driver_standings', $Standings);
}
my %DriverStandings; my %DriverStandings;
my $DriverStandingsFormatted = sprintf( "%2s %3s %-5s%7s\n", my $DriverStandingsFormatted = sprintf( "%2s %3s %-5s%7s\n",
"#", "No.", "Code", "Points" ); "#", "No.", "Code", "Points" );
@ -235,16 +250,19 @@ sub statistics {
my $StatIdentifier = $+{statidentifier}; my $StatIdentifier = $+{statidentifier};
given ($StatIdentifier) { given ($StatIdentifier) {
when ('standings') { when ('standings') {
my $Standings = json POST( my $Standings;
join( if ( $CACHE->get('constructor_standings') ) {
'/', $Standings = $CACHE->get('constructor_standings');
( }
$Self->{URL}{Ergast}, 'current', else {
'constructorStandings.json' $Standings = json POST(
) join( '/',
( $Self->{URL}{Ergast}, 'current', 'constructorStandings.json' )
), ),
{} {}
); );
$CACHE->set('constructor_standings', $Standings);
}
$Self->{LogObject} $Self->{LogObject}
->info( 'statistics: Fetched standings are ' ->info( 'statistics: Fetched standings are '
. Dumper($Standings) ); . Dumper($Standings) );