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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |