Skip to content

Commit

Permalink
Latest Utils.pm from VWF
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Mar 13, 2024
1 parent 596d42e commit 43413c5
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions lib/Geo/Coder/Free/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sub create_disc_cache {
my $logger = $args{'logger'};
my $driver = $config->{disc_cache}->{driver};
unless(defined($driver)) {
my $root_dir = $args{'root_dir'} || $config->{disc_cache}->{root_dir};
my $root_dir = $args{'root_dir'} || $ENV{'root_dir'} || $config->{disc_cache}->{root_dir} || $config->{'root_dir'};
throw Error::Simple('root_dir is not optional') unless($root_dir);

if($logger) {
Expand All @@ -45,10 +45,11 @@ sub create_disc_cache {
namespace => $args{'namespace'}
);

if($logger) {
$chi_args{'on_set_error'} = 'log';
$chi_args{'on_get_error'} = 'log';
}
# Don't do this because it takes a lot of complex configuration
# if($logger) {
# $chi_args{'on_set_error'} = 'log';
# $chi_args{'on_get_error'} = 'log';
# }

if($config->{disc_cache}->{server}) {
my @servers;
Expand All @@ -67,29 +68,28 @@ sub create_disc_cache {
}
}
$chi_args{'servers'} = \@servers;
} elsif(($driver ne 'DBI') && ($driver ne 'Null')) {
$chi_args{'root_dir'} = $args{'root_dir'} || $config->{disc_cache}->{root_dir};
throw Error::Simple('root_dir is not optional') unless($chi_args{'root_dir'});
if($logger) {
$logger->debug("root_dir: $chi_args{root_dir}");
} elsif($driver eq 'DBI') {
# Use the cache connection details in the configuration file
$chi_args{'dbh'} = DBI->connect($config->{disc_cache}->{connect});
if(!defined($chi_args{'dbh'})) {
if($logger) {
$logger->error($DBI::errstr);
}
throw Error::Simple($DBI::errstr);
}
}
if($driver eq 'Redis') {
$chi_args{'create_table'} = 1;
} elsif($driver eq 'Redis') {
my %redis_options = (
reconnect => 60,
every => 1_000_000
);
$chi_args{'redis_options'} = \%redis_options;
} elsif($driver eq 'DBI') {
# Use the cache connection details in the configuration file
$chi_args{'dbh'} = DBI->connect($config->{disc_cache}->{connect});
if(!defined($chi_args{'dbh'})) {
if($logger) {
$logger->error($DBI::errstr);
}
throw Error::Simple($DBI::errstr);
}
$chi_args{'create_table'} = 1;
} elsif($driver ne 'Null') {
$chi_args{'root_dir'} = $args{'root_dir'} || $config->{disc_cache}->{root_dir};
throw Error::Simple('root_dir is not optional') unless($chi_args{'root_dir'});
if($logger) {
$logger->debug("root_dir: $chi_args{root_dir}");
}
}
return CHI->new(%chi_args);
}
Expand All @@ -108,7 +108,7 @@ sub create_memory_cache {
}
# return CHI->new(driver => 'Memcached', servers => [ '127.0.0.1:11211' ], namespace => $args{'namespace'});
# return CHI->new(driver => 'File', root_dir => '/tmp/cache', namespace => $args{'namespace'});
return CHI->new(driver => 'SharedMem', size => 16 * 1024, max_size => 16 * 1024, shmkey => 98766789, namespace => $args{'namespace'});
return CHI->new(driver => 'SharedMem', max_size => 1024, shm_size => 16 * 1024, shm_key => 98766789, namespace => $args{'namespace'});
}
if($logger) {
$logger->debug('memory cache via ', $config->{memory_cache}->{driver}, ', namespace: ', $args{'namespace'});
Expand Down Expand Up @@ -144,18 +144,20 @@ sub create_memory_cache {
}
$chi_args{'servers'} = \@servers;
} elsif($driver eq 'SharedMem') {
$chi_args{'shmkey'} = $args{'shmkey'} || $config->{memory_cache}->{shmkey};
if(my $size = ($args{'size'} || $config->{'memory_cache'}->{'size'})) {
$chi_args{'max_size'} = $chi_args{'size'} = $size;
$chi_args{'shm_key'} = $args{'shm_key'} || $config->{memory_cache}->{shm_key};
if(my $shm_size = ($args{'shm_size'} || $config->{'memory_cache'}->{'shm_size'})) {
$chi_args{'shm_size'} = $shm_size;
}
if(my $max_size = ($args{'max_size'} || $config->{'memory_cache'}->{'max_size'})) {
$chi_args{'max_size'} = $max_size;
}
} elsif(($driver ne 'Null') && ($driver ne 'Memory') && ($driver ne 'SharedMem')) {
$chi_args{'root_dir'} = $args{'root_dir'} || $config->{memory_cache}->{root_dir};
} elsif(($driver ne 'Null') && ($driver ne 'Memory')) {
$chi_args{'root_dir'} = $args{'root_dir'} || $config->{memory_cache}->{root_dir} || $config->{'root_dir'};
throw Error::Simple('root_dir is not optional') unless($chi_args{'root_dir'});
if($logger) {
$logger->debug("root_dir: $chi_args{root_dir}");
}
}
if($driver eq 'Redis') {
} elsif($driver eq 'Redis') {
my %redis_options = (
reconnect => 60,
every => 1_000_000
Expand Down

0 comments on commit 43413c5

Please sign in to comment.