Help language development. Donate to The Perl Foundation

Hash::LRU zef:lizmat last updated on 2021-09-10
[![Actions Status](](


Hash::LRU - trait for limiting number of keys in hashes by usage


    use Hash::LRU;  # Least Recently Used

    my %h is LRU;   # defaults to elements => 100

    my %h is LRU(elements => 42);  # note: value must be known at compile time!

    my %h{Any} is LRU;  # object hashes also supported


Hash::LRU provides a `is LRU` trait on `Hash`es as an easy way to limit the number of keys kept in the `Hash`. Keys will be added as long as the number of keys is under the limit. As soon as a new key is added that would exceed the limit, the least recently used key is removed from the `Hash`.

Both "normal" as well as object hashes are supported.


    use Hash::LRU;

    my %h is LRU(elements => 3);

    %h<name>       = "Alex";
    %h<language>   = "Raku";
    %h<occupation> = "devops";
    %h<location>   = "Russia";

    say %h.raku;
    # {:location("Russia"), :occupation("devops"), :language("Raku")}


Elizabeth Mattijsen <[email protected]>

Source can be located at: . Comments and Pull Requests are welcome.


Copyright 2018, 2020, 2021 Elizabeth Mattijsen

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.