I just started experimenting with mod_perl, BerkeleyDB 4.x, and the BerkeleyDB perl module (bdb for short) on FreeBSD. One of the things I ran into immediately was that when I loaded a page that used bdb to retrieve some content, I heard disk activity every time I reloaded the page. It seemed slow so I measured a pageload rate of 30 pages/sec. I would prefer that the database be opened once and subsequent requests use the open handle. My first thought was to use the following:
our $db;
my $filename = "/usr/local/www/modperl/articles.db";
unless (defined($db)) {
$db = new BerkeleyDB::Btree
-Filename => $filename,
-Flags => DB_CREATE,
-Compare => sub { $_[0] <=> $_[1] }
or die "Cannot open file $filename: $! $BerkeleyDB::Error\n";
}
This code resulted in about 108 pages/sec. I believe that each Apache child process has its own copy of a reference to the db. For a readonly db this would seem to be ok. Does anyone know of any gotchas to this method or something better?