I'm doing a lot of calls to recordset on various tables in a
database. Since I was doing a rather complicated Setup each time, I
put it in a central routine. And then it occured to me that there
might be a benefit in caching the results of the setup, so I added
that. The code looks something like this.
sub setupDB {
my $this = shift;
my (%args) = @_;
my ($db, $set, $connect);
#
# just do the database initialization once and use SaveAs for
# future reference
#
if (!$this->{_db}) {
$connect = 'DBI:' . $this->{dbiString} . ':' . $this->{database};
new DBIx::Database({
'!DataSource' => $connect,
'!DBIAttr' => {RaiseError => 1, AutoCommit => 1},
'!Username' => $this->{username},
'!Password' => $this->{password},
'!KeepOpen' => 1,
'!SaveAs' => 'Articles',
}) || die $DBI::errstr;
$this->{_db} = 'Articles';
my $dateparse = ...
my $timeparse = ...
$this->{_setup} = {
'!DataSource' => $this->{_db},
'!LongNames' => 2,
'!Serial' => 'id',
'!Filter' =>
{
'dtm' => $dateparse,
'dtc' => $dateparse,
'verifydate' => $dateparse,
'validdate' => $dateparse,
'approveddate' => $dateparse,
'pubdate' => $dateparse,
},
};
}
#
# check and see if we've already done the setup
#
return $this->{"_$args{'!Cache'}"}
if ($args{'!Cache'} && $this->{"_$args{'!Cache'}"});
#
# otherwise do it now, merging in the new items with the global ones
#
my %setup = %{$this->{_setup}};
my ($key, $val);
while (($key, $val) = each(%args)) {
next if ($key eq '!Cache');
$setup{$key} = $val;
}
$set = DBIx::Recordset->Setup(\%setup);
$this->{"_$args{'!Cache'}"} = $set if ($args{'!Cache'});
return $set;
}
A call to this looks might look like:
$set = $this->setupDB(
'!Cache' => 'listArticles',
'!Table' => 'article',
);
$args->{'$order'} = 'dtm DESC' if (!$args->{'$order'});
$$set->Search($args);
The idea is to cache the return value from $set, similar to the way
that DBIx::Ddatabase can use !SaveAs to cache the database parsing.
The question is, "Does it gain me anything?" The only drawback I've
run across so far is that the I have to explicitly call Flush if I
want things written out right away. Are there any others?
--
Kee Hinckley - Somewhere.Com, LLC
http://consulting.somewhere.com/
[EMAIL PROTECTED]
I'm not sure which upsets me more: that people are so unwilling to accept
responsibility for their own actions, or that they are so eager to regulate
everyone else's.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]