
LRU::Cache - A Fast Least Recently Used Cache For Perl
Caching is one of those things every application needs eventually. Whether it's DNS lookups, database rows, or computed results, keeping frequently accessed data close avoids expensive recomputation. The classic data structure for this is the Least Recently Used (LRU) cache : a fixed-size store that automatically evicts the oldest unused entry when full. LRU::Cache is a new CPAN module that implements an LRU cache entirely in C via XS. Every operation — get , set , delete , exists — is O(1). No Perl-level hash ties, no linked list objects, no method dispatch on the hot path if you don't want it. Quick Start use LRU:: Cache ; my $cache = LRU::Cache:: new ( 1000 ); $cache -> set (' user:42 ', { name => ' Alice ', role => ' admin ' }); $cache -> set (' user:43 ', { name => ' Bob ', role => ' editor ' }); my $user = $cache -> get (' user:42 '); # promotes to front print $user -> { name }; # "Alice" The constructor takes a single argument: the maximum number of entries. Once the cache hits
Continue reading on Dev.to
Opens in a new tab