CHI::D...Memory(3pm)
π CHI::Driver::RawMemory π
okay bestie, so this is a page from the User Contributed Perl Documentation, and it's all about CHI::Driver::RawMemory β a lil module that lives inside the CHI::D...Memory(3pm) manpage slot. don't ask about the truncated title, it's giving "abbreviated for your convenience" β¨
πΈ NAME πΈ
CHI::Driver::RawMemory β an in-process memory cache that stores direct references, no serializing, no copying, no nothing. it just... points at your stuff. π
π VERSION π
version 0.61 β she's on 0.61 and she's thriving.
β¨ SYNOPSIS β¨
here's the vibe, literally copy-paste this:
use CHI;
my $hash = {};
my $cache = CHI->new( driver => 'RawMemory', datastore => $hash );
my $cache = CHI->new( driver => 'RawMemory', global => 1 );
my $cache = CHI->new( driver => 'RawMemory', global => 0 );
three ways to summon her, pick your fighter π«Ά
π¦ DESCRIPTION π¦
so CHI::Driver::RawMemory is a subclass of CHI::Driver::Memory, right? and the whole thing about her is that she stores references to data structures directly instead of serializing / deserializing them. no cap.
what does that mean for you, bestie? it means she's faster at getting and setting complex data structures. π₯ iconic. but β and this is the plot twist β unlike most drivers, if you modify the original data structure, that modification will affect the data structure stored in the cache, and vice versa. they're literally the same thing. they're soulmates. they share a brain cell.
like, watch this:
my $cache = CHI->new( driver => 'Memory', global => 1 );
my $lst = ['foo'];
$cache->set('key' => $lst); # serializes $lst before storing
$cache->get('key'); # returns ['foo']
$lst->[0] = 'bar';
$cache->get('key'); # returns ['foo']
okay so with the regular Memory driver, she serialized $lst before storing it β so when you changed $lst->[0] to 'bar', the cache was like "not my problem, I have my own copy" and still returned ['foo']. independent queen. π
now the RawMemory version:
my $cache = CHI->new( driver => 'RawMemory', global => 1 );
my $lst = ['foo'];
$cache->set('key' => $lst); # stores $lst directly
$cache->get('key'); # returns ['foo']
$lst->[0] = 'bar';
$cache->get('key'); # returns ['bar']!
SEE THAT. $lst gets stored directly, so the moment you flip $lst->[0] to 'bar', the cache is out here returning ['bar']! with the exclamation mark and everything. they're linked. it's giving "we are one" π
heads up bestie: this is the whole trade-off. speed and shared references vs. isolation. RawMemory does not give you isolation. if you wanted a copy you wanted the other driver, fr fr.
π CONSTRUCTOR OPTIONS π
Same as CHI::Driver::Memory.
that's it. that's the section. go read that one, she's got all the options. β¨
π SIZE AWARENESS π
for the purpose of size-awareness, all items count as size 1 for this driver. why? because the data structures are not serialized, so there's genuinely no good way to determine their size. it's giving "we tried, we can't measure the vibes". π
so instead of measuring bytes, she just counts items. one item = one. here's how that looks:
# Keep a maximum of 10 items in cache
#
my $cache = CHI->new( driver => 'RawMemory', datastore => {}, max_size => 10 );
max_size => 10 = ten items, ten slots, ten little besties, no matter how chunky any single one of them is. π
π ACKNOWLEDGMENTS π
thanks to Yuval Kogman whose Cache::Ref inspired me to do this. king behavior, honestly. π
β SEE ALSO β
CHI::Driver::MemoryCHI
go read both, iykyk.
π AUTHOR π
Jonathan Swartz <swartz@pobox.com>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2021 by Jonathan Swartz.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. periodt. π
and that's the whole page, bestie!! π¦ this was perl v5.32.1, dated 2021-10-15, from the CHI::D...Memory(3pm) manpage β thanks for reading, mwah, go store some references responsibly π«Άβ¨π
π¦ where this page comes from
- debian bookworm: libchi-perl 0.61-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libchi-perl 0.61-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libchi-perl 0.61-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libchi-perl 0.61-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libchi-perl 0.61-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libchi-perl 0.61-1 Β· 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