Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > problem using both Memoize::Expire and DB_File

Reply
Thread Tools

problem using both Memoize::Expire and DB_File

 
 
samwyse
Guest
Posts: n/a
 
      07-20-2005
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:EBUG = 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)
>> Exists 1

Not in underlying hash at all.
inside test2()
>> Store 1 test2(1)

--- test2(1) => test2(1)
>> Exists 1

Not in underlying hash at all.
inside test3()
>> Store 1 test3(1)

--- test3(1) => test3(1)
*** loop 2
--- test0(1) => test0(1)
--- test1(1) => test1(1)
>> Exists 1

Time to live for this item: 60
(Still good)
>> Fetch cached value for 1
>> (ttl: 60, nuses: 65535)

--- test2(1) => test2(1)
>> Exists 1

Time to live for this item: 60
(Still good)
>> Fetch cached value for 1
>> (ttl: 60, nuses: 65535)

--- test3(1) => test3(1)
$ ./test-me.pl
*** loop 1
inside test0()
--- test0(1) => test0(1)
--- test1(1) => test1(1)
>> Exists 1

Not in underlying hash at all.
inside test2()
>> Store 1 test2(1)

--- test2(1) => test2(1)
>> Exists 1

Not in underlying hash at all.
inside test3()
>> Store 1 test3(1)

--- test3(1) => test3(1)
*** loop 2
--- test0(1) => test0(1)
--- test1(1) => test1(1)
>> Exists 1

Time to live for this item: 60
(Still good)
>> Fetch cached value for 1
>> (ttl: 60, nuses: 65535)

--- test2(1) => test2(1)
>> Exists 1

Time to live for this item: 60
(Still good)
>> Fetch cached value for 1
>> (ttl: 60, nuses: 65535)

--- test3(1) => test3(1)
$ exit

script done on Tue Jul 19 17:21:28 2005
 
Reply With Quote
 
 
 
 
samwyse
Guest
Posts: n/a
 
      07-20-2005
samwyse wrote:
> 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.
 
Reply With Quote
 
 
 
 
ioneabu@yahoo.com
Guest
Posts: n/a
 
      07-21-2005

>
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DB_File (hash of array) problem James Perl Misc 5 06-23-2010 01:37 AM
Problem using DB_File Andras Malatinszky Perl Misc 5 04-18-2005 03:43 PM
How do I count keys in a BerkeleyDB *quickly* (using DB_File)? Damon Hastings Perl Misc 2 09-09-2003 09:03 AM
Pointer Artithmetic on DB_File values: i need it (or a very efficient solution for this problem) Antonio Perl 1 07-09-2003 07:25 AM
using 'DB_File' versus just plain tie() ? dan baker Perl Misc 5 07-05-2003 07:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57