Subject: | get method seems to fail when called recursively from within read_cb |
The get method seems to fail when called recursively from within the
read_cb sub. In my application, I'm dealing with heirarchical data,
where each child node contains some information from its parent node.
When the read_cb sub for the child tries to call a Cache::FastMmap
object's get method, it seems that the parent gets cached, but not the
child.
This is in Perl 5.8.4 with Cache::Mmap 1.14 on Fedora Core 6 Linux.
Here's a fairly minimal example which calculates fibonacci numbers. If
I use fastfib, results aren't cached correctly; with slowfib everything
works as expected. A slightly modified version of this works fine with
Cache::Mmap.
#!/usr/bin/perl
use warnings;
use strict;
use Cache::FastMmap;
my $cache = Cache::FastMmap->new(
read_cb => sub {
fastfib($_[1]);
}
);
my $count = 0;
sub fastfib
{
my($n) = @_;
if ($n <= 2)
{
my $r = 1;
return \$r;
}
$count++;
my $a = $cache->get($n-1)
or die "Couldn't calculate fib(@{[$n-1]})\n";
my $b = $cache->get($n-2)
or die "Couldn't calculate fib(@{[$n-2]})\n";
my $sum = $$a+$$b;
return \$sum;
}
sub slowfib
{
my($n) = @_;
if ($n <= 2)
{
my $r = 1;
return \$r;
}
$count++;
my $a = slowfib($n-1)
or die "Couldn't calculate fib(@{[$n-1]})\n";
my $b = slowfib($n-2)
or die "Couldn't calculate fib(@{[$n-2]})\n";
my $sum = $$a+$$b;
return \$sum;
}
print ${$cache->get($ARGV[0])},"\n";
print "($count calculations)\n";