problem using both Memoize::Expire and DB_File

Discussion in 'Perl Misc' started by samwyse, Jul 20, 2005.

  1. samwyse

    samwyse Guest

    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)
    >> 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
    samwyse, Jul 20, 2005
    #1
    1. Advertising

  2. samwyse

    samwyse Guest

    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.
    samwyse, Jul 20, 2005
    #2
    1. Advertising

  3. samwyse

    Guest


    >
    > 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.
    , Jul 21, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tongass Park Neighborhood Association, Juneau Alas

    Cookies expire immediately, not when set to expire

    Tongass Park Neighborhood Association, Juneau Alas, Oct 1, 2009, in forum: ASP General
    Replies:
    2
    Views:
    1,183
    SQLDude
    Nov 24, 2009
  2. dan baker

    using 'DB_File' versus just plain tie() ?

    dan baker, Jul 2, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    107
    Michele Dondi
    Jul 5, 2003
  3. Andras Malatinszky

    Problem using DB_File

    Andras Malatinszky, Apr 18, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    211
    Andras Malatinszky
    Apr 18, 2005
  4. Replies:
    1
    Views:
    106
  5. david
    Replies:
    4
    Views:
    145
    Tad J McClellan
    Jan 29, 2009
Loading...

Share This Page