1 package SL::DBConnect::Cache;
4 use List::MoreUtils qw(apply);
9 my ($package, @args) = @_;
11 my $dbh = $cache{ _args2str(@args) };
13 if (!$dbh->{Active}) {
14 delete $cache{ _args2str(@args) };
22 my ($package, $dbh, @args) = @_;
24 $cache{ _args2str(@args) } = $dbh;
28 my ($package, @args) = @_;
30 my $dbh = $cache{ _args2str(@args) };
45 my ($dbconnect, $dbuser, $dbpasswd, $options, $initial_sql) = @_;
51 join ';', apply { s/([;\\])/\\$1/g } # no collisions if anything contains ;
52 map { $_ => $options->{$_} }
53 sort keys %$options; # deterministic order
55 join ';', apply { $_ //= ''; s/([;\\])/\\$1/g } $dbconnect, $dbuser, $dbpasswd, $options_str, $initial_sql;
66 SL::DBConnect::Cache - cached database handle pool
70 use SL::DBConnect::Cache;
72 my $dbh = SL::DBConnect::Cache->get(@args);
73 SL::DBConnect::Cache->store($dbh, @args);
75 # reset a cached handle
76 SL::DBConnect::Cache->reset($dbh);
78 # close a cached handle and forget it
79 SL::DBConnect::Cache->close($dbh);
81 SL::DBConnect::Cache->clear($dbh);
86 Implements a managed cache for DB connection handles.
88 The same would be possible with C<< DBI->connect_cached >>, but in that case,
89 we would have no control over the cache.
97 Retrieve a connection specified by C<ARGS>.
99 =item * C<store DBH ARGS>
101 Store a connection specified by C<ARGS>.
103 =item * C<reset ARGS>
105 Rollback the connection specified by C<ARGS>.
109 Empties the cache. If handles are not referenced otherwise, they will get
120 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>