Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

some caching issues using CHI

by tomgracey (Beadle)
on Oct 14, 2013 at 11:17 UTC ( #1058170=perlquestion: print w/replies, xml ) Need Help??
tomgracey has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I am trying to use the CHI module but am coming up against erratic behaviour. There may well be good reason for it but I am stumbling along in the dark at the moment and so hope someone can point me in the right direction as to what is going on.

In my main code I create an accessor to the cache. I am using Moose so it looks like:

has chi => (is => 'rw', isa => 'Any', default => sub{ CHI->new( driver => 'Memory', global => 1, serializer => 'Data::Dumper', namespace => 'vbsite' ) } );

(I could probably do better than 'Any' and will get to that when I work out what it should be!) ... and to store items:

$self->chi->set( $key_hashref, $to_store, 'never' );

Note I have experimented with the duration setting with no substantial difference in results.

Now I wrote a short inspector script to tell me what keys were in the cache at the moment when the script is run:

#!/usr/bin/perl use strict; use CHI; my $namespace = 'vbsite'; my $cache = CHI->new( driver => 'Memory', global => 1,serializer => ' +Data::Dumper', namespace => $namespace ); my @keys = $cache->get_keys(namespace => $namespace); print "Content-type: text/plain\n\n"; print "there are ".scalar(@keys)." keys in namespace $namespace:\n"; print $_."\n" foreach(@keys);

So I run the main program until it seems to be caching, then I run the inspector script. Sure enough I get something like:

there are 3 keys in namespace vbsite: {"dir":"/var/www/html","filename":"intro.xml":"panel_id":"1"} {"dir":"/var/www/html","filename":"menu.xml":"panel_id":"2"} {"dir":"/var/www/html","filename":"home.xml":"panel_id":"3"}

which looks good. But then if I keep running the inspector script (without changing anything at all, without running the main program again or any other programs) I get a different result:

there are 2 keys in namespace vbsite: {"dir":"/var/www/html","filename":"intro.xml":"panel_id":"1"} {"dir":"/var/www/html","filename":"menu.xml":"panel_id":"2"}

... and in fact if I run it over and over, I seem to get a different result each time. Now I dont know if it is designed so ->get_keys only returns a key if the cache would have returned a cached value if it was requested at that moment, and does not return a key if the cache decides this particular request should miss the cache.

However it is causing me problems because, when my main code performs a 'save' operation I would like to inspect all keys in the cache and remove those items that would be affected by the saved file. Because ->get_keys does not seem to reliably return all the keys in the cache this doesnt seem to work

Alternatively perhaps it *should* consistently return all keys in the cache, and there is something wrong with my implementation? Though my implementation really is not complex and it is difficult to see what I could be doing to cause the error. Can anyone shed any light? Your help greatly appreciated...

Replies are listed 'Best First'.
Re: some caching issues using CHI
by ww (Archbishop) on Oct 14, 2013 at 14:04 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1058170]
Approved by hdb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2017-06-26 01:09 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.