Skip to content

Commit 864cb27

Browse files
committed
Cache improvements
1 parent 68a2d48 commit 864cb27

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

src/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,5 @@ grepcpan:
7575
git: '/home/atoomic/bin/git'
7676
cache:
7777
directory: '~APPDIR~/var/tmp'
78-
version: '2.02'
78+
version: '2.10'
7979

src/lib/GrepCpan/Grep.pm

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use Simple::Accessor qw{
2525
use POSIX qw{:sys_wait_h setsid};
2626
use Proc::ProcessTable ();
2727
use Time::HiRes ();
28+
use File::Path ();
2829
use File::Slurp ();
2930
use IO::Handle ();
3031
use Fcntl qw(:flock SEEK_END);
@@ -95,22 +96,20 @@ sub _build_cpan_index_at($self) {
9596
chomp $out;
9697
$out =~ s{['"]}{}g;
9798

98-
return $out; # . ' ' . $self->HEAD;
99+
return $out;
99100
}
100101

101102
sub _build_cache($self) {
102103

103-
# also use HEAD ?? FIXME
104104
my $dir
105-
= ( $self->config()->{'cache'}->{'directory'} ) . '/'
106-
. ( $self->config()->{'cache'}->{'version'} || 0 )
107-
. '/HEAD-'
108-
. $self->HEAD;
109-
die unless $dir;
105+
= $self->_current_cache_version_directory() . '/HEAD-' . $self->HEAD;
110106

111107
$dir = $self->massage_path($dir);
112-
local $ENV{PATH} = '/bin:' . ( $ENV{PATH} // '' );
113-
qx{mkdir -p $dir};
108+
109+
return $dir if -d $dir;
110+
111+
File::Path::make_path( $dir, { mode => 0711, } )
112+
or die "Failed to create $dir: $!";
114113
die unless -d $dir;
115114

116115
# cleanup after directory structure creation
@@ -119,6 +118,12 @@ sub _build_cache($self) {
119118
return $dir;
120119
}
121120

121+
sub _current_cache_version_directory($self) {
122+
123+
return ( $self->config()->{'cache'}->{'directory'} ) . '/'
124+
. ( $self->config()->{'cache'}->{'version'} || 0 );
125+
}
126+
122127
sub _build_root($self) {
123128

124129
# hard code root dir in production
@@ -133,11 +138,9 @@ sub cache_cleanup( $self, $current_cachedir = undef ) { # aka tmpwatch
133138

134139
my @path = split qr{/}, $current_cachedir;
135140

136-
{ # purge old cache versions
137-
my @tmp = @path;
138-
pop @tmp for 1 .. 2;
141+
if ( my $cache_root = $self->config()->{'cache'}->{'directory'} ) {
139142

140-
my $cache_root = join '/', @tmp;
143+
# purge old cache versions
141144
if ( opendir( my $tmp_dh, $cache_root ) ) {
142145
foreach my $dir ( readdir($tmp_dh) ) {
143146
next if $dir eq '.' || $dir eq '..';
@@ -149,18 +152,16 @@ sub cache_cleanup( $self, $current_cachedir = undef ) { # aka tmpwatch
149152
next unless -d $fdir;
150153
next unless length $fdir > 5;
151154

152-
local $ENV{PATH} = '/bin:' . ( $ENV{PATH} // '' );
153-
qx{rm -rf $fdir}
154-
; # kind of dangerous but should be ok, we are controlling these values
155+
# kind of dangerous but should be ok, we are controlling these values
156+
File::Path::remove_tree( $fdir, { safe => 1 } )
157+
or warn "Failed to remove $fdir: $!";
155158
}
156-
close($tmp_dh);
157159
}
158160
}
159161

160-
{ # purge old HEAD directories for the same version
161-
my @tmp = @path;
162-
pop @tmp;
163-
my $version_cache = join '/', @tmp;
162+
if ( my $version_cache = $self->_current_cache_version_directory() ) {
163+
164+
# purge old HEAD directories for the same version
164165
if ( opendir( my $tmp_dh, $version_cache ) ) {
165166
foreach my $dir ( readdir($tmp_dh) ) {
166167
next if $dir eq '.' || $dir eq '..';
@@ -169,8 +170,9 @@ sub cache_cleanup( $self, $current_cachedir = undef ) { # aka tmpwatch
169170
next unless -d $fdir;
170171
next if $fdir eq $current_cachedir;
171172

172-
local $ENV{PATH} = '/bin:' . ( $ENV{PATH} // '' );
173-
qx{rm -rf $fdir}; # purge old cache, in the same weird fashion
173+
# purge old cache, in the same weird fashion
174+
File::Path::remove_tree( $fdir, { safe => 1 } )
175+
or warn "Failed to remove $fdir: $!";
174176
}
175177
}
176178
}
@@ -180,7 +182,7 @@ sub cache_cleanup( $self, $current_cachedir = undef ) { # aka tmpwatch
180182

181183
sub massage_path ( $self, $s ) {
182184

183-
return unless defined $s;
185+
return unless length $s;
184186

185187
my $appdir = $self->root;
186188
$appdir =~ s{/(?:bin|t)/?$}{};
@@ -560,11 +562,11 @@ sub _save_cache ( $self, $cache_file, $cache ) {
560562

561563
sub _get_cache_file ( $self, $keys, $type = undef ) {
562564

563-
$type //= q[/search-ls];
565+
$type //= q[search-ls];
564566
$type .= '-';
565567

566568
my $cache_file
567-
= ( $self->cache() // '' )
569+
= ( $self->cache() // '' ) . '/'
568570
. $type
569571
. md5_hex( join( q{|}, map { defined $_ ? $_ : '' } @$keys ) )
570572
. '.cache';
@@ -615,11 +617,6 @@ sub _get_match_cache(
615617

616618
$caseinsensitive //= 0;
617619

618-
my @keys_for_cache = (
619-
$search, $search_distro, $query_filetype,
620-
$caseinsensitive, $ignore_files // ''
621-
);
622-
623620
my $gitdir = $self->git()->work_tree;
624621
my $limit = $self->config()->{limit}->{files_per_search} or die;
625622

@@ -628,6 +625,12 @@ sub _get_match_cache(
628625
push @git_cmd, q{-i} if $caseinsensitive;
629626
push @git_cmd, $flavor, '-e', $search, q{--}, q{distros/};
630627

628+
my @keys_for_cache = (
629+
$flavor, $caseinsensitive ? 1 : 0,
630+
$search, $search_distro, $query_filetype,
631+
$caseinsensitive, $ignore_files // ''
632+
);
633+
631634
# use the full cache when available -- need to filter it later
632635
my $request_cache_file = $self->_get_cache_file( \@keys_for_cache );
633636
if ( my $load = $self->_load_cache($request_cache_file) ) {

0 commit comments

Comments
 (0)