Tie::CacheHash is a Perl module that maintains sorted lists of top entries.
tie %hash1, 'Tie::CacheHash', 10, 100;
tie %hash2, 'Tie::CacheHash', '5%', '10%';
Of course you can get the "top 100" entries of any perl hash:
@top_keys = (sort my_sort_func keys %my_hash)[0..99];
But if your hash has more than a few thousand entries, that sort operation may take several seconds. And if you have tens of thousands of entries, the sort may take many minutes.
(If you are reading this documentation past the expiration date on the bottom of the carton, please adjust the numbers accordingly. Sorting is always problematic for sufficiently large n.)
Many programs will need to keep track of a "top 100" (or "bottom 100") to perform such operations as expiring the oldest items out of a cache. Sorting the entire array and skimming off the top items is not always an acceptable algorithm. Tie::CacheHash provides a simple and reasonably efficient solution. Its primary design goal is reasonable responsiveness on every operation, i.e. no unpredictable long delays, and it achieves this goal by avoiding the sorting of huge arrays.
The two parameters you pass after the classname are the minimum and maximum allowable size for the cache. The largest array the module will ever have to sort will be somewhat above the maximum (how much depends on the distribution of your data), so picking a good 'max' will help control the maximum delay you will experience.
A 'min' of 0 means it is OK for the cache to run dry and never replenish itself (###I think###), so you probably want a minimum of at least 1. A minimum/maximum of a very large integer (try 2**30) means to keep the whole hash in the cache.
Duplicate values are allowed; if you don't specify your own sort function, they will be secondarily sorted by key.
If you pass in a subhash, you MUST NOT alter its data directly: only through the CacheHash.