I was interested so I performed a bit of an experiment. If I understand correctly, this is similar to the setup you are aiming for:

#!/usr/bin/perl use strict; use warnings; use 5.010; my @az = 'a'..'z'; my %cache = ( a => 1, b => 1, c => 1 ); for (1..20) { my $key = each %cache || ( say "back to front!" and each %cache ); say $key; delete $cache{$key}; $key = $az[rand @az] . $key; $cache{$key} = 1; }

Sample Result:

c a b sa qsa zc back to front! zqsa zzc wb izzc ezqsa cizzc nwb nnwb eezqsa back to front! icizzc bnnwb neezqsa back to front! sneezqsa jicizzc

So, you don't get perfect "oldest first" behavior. In particular, it is fully possible that you evict the most recently cached item. In the bus analogy, if any new cache items sit in a seat behind where the conductor is, they may be evicted prematurely. However, some amount of "oldest first" does seem to exist and from what you've said, this is probably "good enough" for your needs.

I do wonder whether adding items might eventually/occasionally cause perl to recompute the hash which might reset the conductor or cause an "old" key to be moved to a seat in front of the conductor's location so that the old key gets missed in a pass.

Good Day,
    Dean


In reply to Re^3: Using 'each' to get random (key, value) pair from hash returns "uninitialized value" by duelafn
in thread Using 'each' to get random (key, value) pair from hash returns "uninitialized value" by puterboy

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, details, 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, summary, 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.