kivitendo/SL/DBConnect/Cache.pm @ 8e7908eb
ffa9f969 | Sven Schöling | package SL::DBConnect::Cache;
|
||
use strict;
|
||||
use List::MoreUtils qw(apply);
|
||||
my %cache;
|
||||
sub get {
|
||||
my ($package, @args) = @_;
|
||||
my $dbh = $cache{ _args2str(@args) };
|
||||
if (!$dbh->{Active}) {
|
||||
delete $cache{ _args2str(@args) };
|
||||
$dbh = undef;
|
||||
}
|
||||
return $dbh;
|
||||
}
|
||||
sub store {
|
||||
my ($package, $dbh, @args) = @_;
|
||||
$cache{ _args2str(@args) } = $dbh;
|
||||
}
|
||||
sub reset {
|
||||
my ($package, @args) = @_;
|
||||
my $dbh = $cache{ _args2str(@args) };
|
||||
return unless $dbh;
|
||||
$dbh->rollback;
|
||||
$dbh;
|
||||
}
|
||||
sub clear {
|
||||
%cache = ();
|
||||
}
|
||||
sub _args2str {
|
||||
my (@args) = @_;
|
||||
my ($dbconnect, $dbuser, $dbpasswd, $options) = @_;
|
||||
$dbconnect //= '';
|
||||
$dbuser //= '';
|
||||
$dbpasswd //= '';
|
||||
$options //= {};
|
||||
my $options_str =
|
||||
join ';', apply { s/([;\\])/\\$1/g } # no collisions if anything contains ;
|
||||
map { $_ => $options->{$_} }
|
||||
sort keys %$options; # deterministic order
|
||||
join ';', apply { s/([;\\])/\\$1/g } $dbconnect, $dbuser, $dbpasswd, $options_str;
|
||||
}
|
||||
1;
|
||||
__END__
|
||||
=encoding utf-8
|
||||
=head1 NAME
|
||||
SL::DBConnect::Cache - cached database handle pool
|
||||
=head1 SYNOPSIS
|
||||
07062a3c | Geoffrey Richardson | use SL::DBConnect::Cache;
|
||
ffa9f969 | Sven Schöling | |||
07062a3c | Geoffrey Richardson | my $dbh = SL::DBConnect::Cache->get(@args);
|
||
SL::DBConnect::Cache->store($dbh, @args);
|
||||
ffa9f969 | Sven Schöling | |||
# reset a cached handle
|
||||
07062a3c | Geoffrey Richardson | SL::DBConnect::Cache->reset($dbh);
|
||
ffa9f969 | Sven Schöling | |||
# close a cached handle and forget it
|
||||
07062a3c | Geoffrey Richardson | SL::DBConnect::Cache->close($dbh);
|
||
ffa9f969 | Sven Schöling | |||
07062a3c | Geoffrey Richardson | SL::DBConnect::Cache->clear($dbh);
|
||
ffa9f969 | Sven Schöling | |||
=head1 DESCRIPTION
|
||||
07062a3c | Geoffrey Richardson | Implements a managed cache for DB connection handles.
|
||
ffa9f969 | Sven Schöling | |||
The same would be possible with C<< DBI->connect_cached >>, but in that case,
|
||||
07062a3c | Geoffrey Richardson | we would have no control over the cache.
|
||
ffa9f969 | Sven Schöling | |||
=head1 METHODS
|
||||
=over 4
|
||||
=item * C<get ARGS>
|
||||
Retrieve a connection specified by C<ARGS>.
|
||||
=item * C<store DBH ARGS>
|
||||
Store a connection specified by C<ARGS>.
|
||||
=item * C<reset ARGS>
|
||||
Rollback the connection specified by C<ARGS>.
|
||||
=item * C<clear>
|
||||
07062a3c | Geoffrey Richardson | Empties the cache. If handles are not referenced otherwise, they will get
|
||
ffa9f969 | Sven Schöling | dropped and closed.
|
||
=back
|
||||
=head1 BUGS
|
||||
None yet :)
|
||||
=head1 AUTHOR
|
||||
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
|
||||
=cut
|