Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone tell me what I'm doing wrong?
#!/usr/bin/perl -w use Cache::MemoryCache; my $cache = new Cache::MemoryCache(); my $customer = $cache->get($ARGV[0]); if( not defined $customer ){ print "setting cache\n"; $customer->{'email_address'} = 'foo@foo.com'; $customer->{'first_name'} = 'foo'; $customer->{'last_name'} = 'bar'; $customer->{'id'} = $ARGV[0]; $cache->set($ARGV[0], $customer, "10 minutes" ); }
For some reason I can get the FileCache to work but not the memory Cache. This works just fine in the "Default" Namespace
#!/usr/bin/perl -w use Cache::FileCache; my $cache = new Cache::FileCache(); my $customer = $cache->get($ARGV[0]); if( not defined $customer ){ print "setting cache\n"; $customer->{'email_address'} = 'foo@foo.com'; $customer->{'first_name'} = 'foo'; $customer->{'last_name'} = 'bar'; $customer->{'id'} = $ARGV[0]; $cache->set($ARGV[0], $customer, "10 minutes" ); }

Replies are listed 'Best First'.
Re: Cache::MemoryCache
by diotalevi (Canon) on Jul 19, 2003 at 00:31 UTC

    What do you mean by "it doesn't work"? The memory cache goes away when the perl program exits. For your example the cache is *always* empty when the script starts. You have to use a FileCache or something similar if you want it to keep its data between calls to your program.

Re: Cache::MemoryCache
by sauoq (Abbot) on Jul 19, 2003 at 00:34 UTC

    FileCache is persistent across processes. MemoryCache isn't.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Is there a way for me to make it persistent? I want to hit the db, populate the object and cache it in memory and avoid having to hit the db again, next time I have a request for that object.

        maybe, just maybe you can use Cache::SharedMemoryCache

        have one program that that stays running to hold the cache in memory while other programs come and go.

        # holdcache.pl use Cache::SharedMemoryCache; my %cache_options_= ( 'namespace' => 'MyNamespace', ); my $c = Cache::SharedMemoryCache->new( \%cache_options ) or die( "Couldn't instantiate SharedMemoryCache" ); while (1) { sleep 300; } __END__

        should do to keep the cache open.

        $ perl holdcache.pl & $ perl -e 'use Cache::SharedMemoryCache;$c=Cache::SharedMemoryCache->n +ew({namespace=>"MyNamespace"}) or die;$c->set("foo",1);' $ perl -e 'use Cache::SharedMemoryCache;$c=Cache::SharedMemoryCache->n +ew({namespace=>"MyNamespace"}) or die;$x=$c->get("foo");print $x,$/' 1

        presto!

        Use FileCache, then its persistent.

        Is there a way for me to make it persistent?

        Uh... no. You could get a close approximation by using FileCache and a RAM disk.

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Cache::MemoryCache
by fglock (Vicar) on Jul 19, 2003 at 02:46 UTC

    You can use a two level cache.

    #!/usr/bin/perl -w use Cache::MemoryCache; use Cache::FileCache; my $file_cache = new Cache::FileCache(); my $mem_cache = new Cache::MemoryCache(); my $customer = $mem_cache->get($ARGV[0]); if( not defined $customer ){ $customer = $file_cache->get($ARGV[0]); if( not defined $customer ){ print "setting cache\n"; $customer->{'email_address'} = 'foo@foo.com'; $customer->{'first_name'} = 'foo'; $customer->{'last_name'} = 'bar'; $customer->{'id'} = $ARGV[0]; $file_cache->set($ARGV[0], $customer, "10 minutes" ); } $mem_cache->set($ARGV[0], $customer, "10 minutes" ); }

    Note: untested, and may contain logic errors.