Subject: | prepare_cached - using a tied hash |
Hello, (and thanks for DBI)
I hope you can help me clarify some behaviour.
In DBI.pm (version 1.601) in the section on prepare_cached, I'm
wondering if:
If you'd like the cache to managed intelligently, you can tie the
hashref returned by CachedKids to an appropriate caching module, such as
Tie::Cache::LRU:
my $cache = $dbh->{CachedKids};
tie %$cache, 'Tie::Cache::LRU', 500;
should be:
If you'd like the cache to be managed intelligently, you can tie the
hashref returned by CachedKids to an appropriate caching module, such as
Tie::Cache::LRU:
my $cache;
tie %$cache, 'Tie::Cache::LRU', 500;
$dbh->STORE('CachedKids',$cache);
I can only get it to use the tied hash if I do the later.
Here is my test code, I have a modified version of Tie::Cache::LRU that
is printing to STDERR when Tie::Cache::LRU::Array FETCH is called.
use strict;
use warnings;
use DBI;
use Data::Dumper;
use Tie::Cache::LRU;
my $dsn = 'dbi:mysql:database=test;host=<hostname>;port=3306';
my $dbh = DBI->connect( $dsn, '<username>', '<password>' );
my $cache = $dbh->{CachedKids};
tie %$cache, 'Tie::Cache::LRU', 500;
# I need to uncomment this to ensure CachedKids is TRUE
# and prepare_cached does not set it to {}
#$dbh->STORE('CachedKids', $cache);
print "->{CachedKids} is ";
if($dbh->{CachedKids}){
print "TRUE\n";
} else {
print "FALSE\n";
}
print Dumper($dbh->FETCH('CachedKids'));
print "FETCH('CachedKids') is ";
if($dbh->FETCH('CachedKids')){
print "TRUE\n";
} else {
print "FALSE\n";
}
my $sth = $dbh->prepare_cached( q{SELECT 'a'} );
$sth->execute();
my $rs = $sth->fetchall_arrayref( {} );
$sth->finish();
I'm not sure if it's the line:
my $cache = $dbh->{CachedKids} ||= {};
in prepare_cached and/or the init value of $dbh->{CachedKids} and/or
the way the tie is being done. I'm testing using version 0.21 of
Tie::Cache::LRU from 2002 with Perl 5.8.7 on x86_64-linux.
Thanks,
Peter (Stig) Edwards