Tie::C...irtual(3pm)
π NAME π
okay so bestie, this whole page is Tie::C...irtual(3pm) from the User Contributed Perl Documentation, and the thing it's documenting is Tie::Cache::LRU::Virtual β literally the virtual base class for all the Tie::Cache::LRU::* girlies. π
β¨ SYNOPSIS β¨
the vibe is: you make your own package, you inherit, you fill in the blanks π¦
package My::Tie::Cache::LRU;
use base qw(Tie::Cache::LRU::Virtual);
...override and define key methods...
πΈ DESCRIPTION πΈ
so this is a pure virtual base class β meaning it defines the public methods of Tie::Cache::LRU and that's basically it. the whole plan is that you subclass off of it and then go fill in all the missing / incomplete methods yourself. no cap, it's giving "here's the blueprint, you build the house". ποΈ
you must implement the entire hash interface, every single one:
TIEHASHCLEARFETCHSTOREEXISTSDELETEFIRSTKEYNEXTKEY
and the object interface too:
curr_sizemax_size
plus DESTROY if you need it. π
the author is like "I'm usually not taken to such heights of OO formality, but in this case a virtual class seemed in order." β and honestly? iconic. respect the commitment to the bit. π
π« USAGE π«
the cache is extremely simple β it literally just holds a simple scalar. that's it. that's the whole thing.
so if you wanna cache an object, just toss it in there:
$cache{$obj->id} = $obj;
heads up! this does not make a copy of the object. it just holds a reference to it. (Note: this means your object's destructor will not be called until it has fallen out of the cache β and all other references to it have disappeared too, obviously!) π
wanna cache an array instead? drop a reference to it in:
$cache{$some_id} = \@array;
or, if you're stressed about the consequences of flinging references around everywhere and you'd rather cache an actual copy, you can do something like this:
$cache{$some_id} = [@array];
π§ Tied Interface
tie
tie %cache, 'Tie::Cache::LRU';
tie %cache, 'Tie::Cache::LRU', $cache_size;
this ties a cache to %cache that will hold a maximum of $cache_size keys. if you don't hand over a $cache_size, it uses a default value: Tie::Cache::LRU::DEFAULT_MAX_SIZE.
if the size is set to 0, the cache is effectively turned off. which is actually so useful β you can "remove" the cache from a program without having to make deep alterations to the program itself, or you can use it to check performance differences with and without a cache. main character move fr fr. β¨
all of the expected hash operations (exists, delete, slices, etc...) work on the %cache.
π¦ Object Interface
there's a few things you just can't do through the tied interface. to do those, you need to get at the underlying object, which you do with tied().
$cache_obj = tied %cache;
and then you can call a few methods on that object:
max_size
$cache_obj->max_size($size);
$size = $cache_obj->max_size;
an accessor to alter the maximum size of the cache on the fly. π
if max_size() is reset and it's lower than the current size, the cache is immediately truncated.
the size must be an integer greater than or equal to 0.
curr_size
$size = $cache_obj->curr_size;
returns the current number of items in the cache. that's it, that's the method. π«Ά
π AUTHOR π
Michael G Schwern <schwern@pobox.com>
π SEE ALSO π
Tie::Cache::LRUTie::Cache::LRU::LinkedListTie::Cache::LRU::ArrayTie::Cache
and that's the whole page, bestie π this was perl v5.36.0, dated 2022-08-29 β go forth and implement those methods, you got this. periodt. π
β¨
π¦ where this page comes from
- ubuntu noble: libtie-cache-lru-perl 20150301-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtie-cache-lru-perl 20150301-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtie-cache-lru-perl 20150301-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms