Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [Discipulus]: śledzie!! the only sweet my father in law eat

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (6)
    As of 2017-11-21 11:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:













      Results (297 votes). Check out past polls.

      Notices?