Menu

πŸ“°
0

LRU::Cache - A Fast Least Recently Used Cache For Perl

DEV Community: perlΒ·LNATIONΒ·about 1 month ago
#lQU0zvcM
#dev#class#code#cache#highlight#article
Reading 0:00
15s threshold

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.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More