Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm not sure the bug has a good fix.

There's a method compute which takes a key and a callback parameter. If there's no value for the given key, the callback is used to create the value and stored in the cache.

The problem is the method is context sensitive, i.e. it uses wantarray, propagates the context to the callback, and stores its return value in a different way based on the context.

But it's perfectly fine to store a value in a cache in scalar context and later retrieve it in list context - and the current implementation might fail, because it tries to apply array dereference in list context. Moreover, if the callback is context sensitive and returns something else in scalar versus list context, you never know which value is stored in the cache - it depends on which context you first called compute in.

What should we do? Calling the callback twice to store both the values is crazy (and might break code if it uses the callback to e.g. generate a unique id).

If it was upon me to decide, I'd remove context sensitivity from the method. If you want to store an array reference, provide a callback that does so.

Just for illustration, here's a test that passes with the current implementation (suitable to be pasted into lib/CHI/t/Bugs.pm):

sub test_141024 : Tests { my $cache = CHI->new( driver => 'RawMemory', global => 1 ); my $callback = sub { wantarray ? 'list' : 'scalar' }; my $scalar = $cache->compute( 1, undef, $callback ); my @array = $cache->compute( 2, undef, $callback ); is_deeply( $cache->get(1), 'scalar' ); is_deeply( $cache->get(2), ['list'] ); eval { my @array2 = $cache->compute( 1, undef, $callback ) }; like( $@, qr/Can't use string \("scalar"\) as an ARRAY ref while " +strict refs"/ ); $cache->set( 3, ['in_list'] ); is_deeply( $cache->get(3), ['in_list'] ); my @foo = map $cache->compute( $_, undef, $callback ), 3, 4; is_deeply( $foo[0], 'in_list' ); is_deeply( $foo[1], 'list' ); }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re^6: Big cache -- CHI Unified cache handling interface by choroba
in thread Big cache by Liebranca

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 chilling in the Monastery: (2)
As of 2024-04-25 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found