problem using both Memoize::Expire and DB_File

S

samwyse

Following the example in the Memoize::Expire perldoc, I tried to set up
a disk-backed cache with an expiration time. It doesn't seem to want to
work for me. Below is my test script and its results. Subroutine
'test0' is memoized via a simple cache, 'test1' uses a disk-backed
cache, 'test2' uses an expiring cache, and 'test3' uses both. The test
program calls each subroutine twice and the program itself is run twice;
the problem is evident during the first pass of the second run, where
'test1' doesn't get invoked (as expected, since the value is found in
the disk-based cache) but 'test3' is invoked. (Specifically, the
message "inside test3()" shouldn't be seen in loop 1 of the second run.)

Using 'od -c' against the db files indicates that the data for 'test3'
is never written to disk. Google doesn't seem to turn anything up,
except for lots of copies of the perldoc. I'm running this on a Sun
under Solaris 2.6. My Perl version is 5.6.X (Sorry, I forgot to note
the exact value and I'm at home right now.), and upgrading to 5.8 would
be difficult on this server.

Later today I plan to dig into the internals of Memoize::Expire. Until
then, thanks for any assistance.


Script started on Tue Jul 19 17:21:03 2005
$ cat test-me.pl
#!/usr/bin/perl

use strict;
use Memoize; # Make your functions faster by trading space for time
use Memoize::Expire; # automatic expiration of memoized values
use DB_File;

$Memoize::Expire::DEBUG = 1;
my $lifetime = 60;

sub test0 {
print "inside test0()\n";
return "test0($_[0])";
}
my %test0_hash;
memoize 'test0',
LIST_CACHE => 'FAULT',
SCALAR_CACHE => [ HASH => \%test0_hash ];

sub test1 {
print "inside test1()\n";
return "test1($_[0])";
}
# Set up persistence
tie my %test1_dbfile => 'DB_File',
'/tmp/test1.db', O_CREAT|O_RDWR, 0666;
# Set up memoization, supplying expiring persistent hash for cache
memoize 'test1',
LIST_CACHE => 'FAULT',
SCALAR_CACHE => [ HASH => \%test1_dbfile ];

sub test2 {
print "inside test2()\n";
return "test2($_[0])";
}
my %test2_hash;
# Set up expiration policy, supplying persistent hash as a target
tie my %test2_cache => 'Memoize::Expire',
LIFETIME => $lifetime, # In seconds
HASH => \%test2_hash;
# Set up memoization, supplying expiring persistent hash for cache
memoize 'test2',
LIST_CACHE => 'FAULT',
SCALAR_CACHE => [ HASH => \%test2_cache ];

sub test3 {
print "inside test3()\n";
return "test3($_[0])";
}
# Set up persistence
tie my %test3_dbfile => 'DB_File',
'/tmp/test3.db', O_CREAT|O_RDWR, 0666;
# Set up expiration policy, supplying persistent hash as a target
tie my %test3_cache => 'Memoize::Expire',
LIFETIME => $lifetime, # In seconds
HASH => \%test3_dbfile;
# Set up memoization, supplying expiring persistent hash for cache
memoize 'test3',
LIST_CACHE => 'FAULT',
SCALAR_CACHE => [ HASH => \%test3_cache ];

for (1..2) {
print "*** loop $_\n";
print "--- test0(1) => ", scalar test0(1), "\n";
print "--- test1(1) => ", scalar test1(1), "\n";
print "--- test2(1) => ", scalar test2(1), "\n";
print "--- test3(1) => ", scalar test3(1), "\n";
}
$ rm /tmp/test*.db
$ ./test-me.pl
*** loop 1
inside test0()
--- test0(1) => test0(1)
inside test1()
--- test1(1) => test1(1)Not in underlying hash at all.
inside test2()Not in underlying hash at all.
inside test3()--- test3(1) => test3(1)
*** loop 2
--- test0(1) => test0(1)
--- test1(1) => test1(1)Time to live for this item: 60
(Still good)Time to live for this item: 60
(Still good)--- test3(1) => test3(1)
$ ./test-me.pl
*** loop 1
inside test0()
--- test0(1) => test0(1)
--- test1(1) => test1(1)Not in underlying hash at all.
inside test2()Not in underlying hash at all.
inside test3()--- test3(1) => test3(1)
*** loop 2
--- test0(1) => test0(1)
--- test1(1) => test1(1)Time to live for this item: 60
(Still good)Time to live for this item: 60
(Still good)--- test3(1) => test3(1)
$ exit

script done on Tue Jul 19 17:21:28 2005
 
S

samwyse

samwyse said:
Following the example in the Memoize::Expire perldoc, I tried to set up
a disk-backed cache with an expiration time. It doesn't seem to want to
work for me. Below is my test script and its results. Subroutine
'test0' is memoized via a simple cache, 'test1' uses a disk-backed
cache, 'test2' uses an expiring cache, and 'test3' uses both. The test
program calls each subroutine twice and the program itself is run twice;
the problem is evident during the first pass of the second run, where
'test1' doesn't get invoked (as expected, since the value is found in
the disk-based cache) but 'test3' is invoked. (Specifically, the
message "inside test3()" shouldn't be seen in loop 1 of the second run.)

Using 'od -c' against the db files indicates that the data for 'test3'
is never written to disk. Google doesn't seem to turn anything up,
except for lots of copies of the perldoc. I'm running this on a Sun
under Solaris 2.6. My Perl version is 5.6.X (Sorry, I forgot to note
the exact value and I'm at home right now.), and upgrading to 5.8 would
be difficult on this server.

Later today I plan to dig into the internals of Memoize::Expire. Until
then, thanks for any assistance.

OK, I've confirmed that the same behaivor exists for Perl 5.8.6 running
under Cygwin. It also doesn't seem to matter if I use DB_File or
Memoize::AnyDBM_File. I guess that I'll be firing off a note to the
package maintainer.
 
I

ioneabu

OK, I've confirmed that the same behaivor exists for Perl 5.8.6 running
under Cygwin. It also doesn't seem to matter if I use DB_File or
Memoize::AnyDBM_File. I guess that I'll be firing off a note to the
package maintainer.

Probably too specialized for this group. Get on the module mailing
list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top