Tie::C...irtual(3pm)

πŸ“¦
this version Β· 2 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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:

  • TIEHASH
  • CLEAR
  • FETCH
  • STORE
  • EXISTS
  • DELETE
  • FIRSTKEY
  • NEXTKEY

and the object interface too:

  • curr_size
  • max_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::LRU
  • Tie::Cache::LRU::LinkedList
  • Tie::Cache::LRU::Array
  • Tie::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