Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to some caching issues using CHI by tomgracey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-28 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found