Skip to content

Commit ce90853

Browse files
committed
Cache the current_version for 10 min
1 parent c0c7140 commit ce90853

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

lib/GrepCpan/Grep.pm

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -431,21 +431,32 @@ sub update_match_counter($cache) { # dead
431431
}
432432

433433
sub current_version($self) {
434+
my $now = time();
435+
my $cache_ttl = 600; # 10 minutes in seconds
434436

435-
# cache the current grep metacpan version
436-
$self->{__version__} //= join(
437-
'-',
438-
$grepcpan::VERSION,
439-
'cache' => $self->config()->{'cache'}->{'version'},
440-
'grep' => eval {
441-
scalar Git::Repository->new(
442-
work_tree => $self->root,
443-
{ git => $self->git_binary }
444-
)->run(qw{rev-parse --short HEAD});
445-
} // '',
446-
'cpan' => eval { scalar $self->git->run(qw{rev-parse --short HEAD}) }
447-
// '',
448-
);
437+
# Check if we need to refresh the cache
438+
if ( !exists $self->{__version__}
439+
|| !exists $self->{__version_timestamp__}
440+
|| ( $now - $self->{__version_timestamp__} ) > $cache_ttl )
441+
{
442+
443+
$self->{__version__} = join(
444+
'-',
445+
$grepcpan::VERSION,
446+
'cache' => $self->config()->{'cache'}->{'version'},
447+
'grep' => eval {
448+
scalar Git::Repository->new(
449+
work_tree => $self->root,
450+
{ git => $self->git_binary }
451+
)->run(qw{rev-parse --short HEAD});
452+
} // '',
453+
'cpan' =>
454+
eval { scalar $self->git->run(qw{rev-parse --short HEAD}) }
455+
// '',
456+
);
457+
458+
$self->{__version_timestamp__} = $now;
459+
}
449460

450461
return $self->{__version__};
451462
}

0 commit comments

Comments
 (0)