Mojo::Cache(3)

✨ Mojo::Cache ✨

Okay bestie, so this is a Perl module β€” it lives over in the User Contributed Perl Documentation section of the manual, and honestly? It's kind of a vibe. Let's get into it πŸ’…


πŸŽ€ NAME πŸŽ€

Mojo::Cache β€” a naive in-memory cache. Yes, "naive" is literally in the name, she's honest about it, we respect that 🫢


πŸ¦‹ SYNOPSIS πŸ¦‹

Here's the whole flex, copy it exactly or it won't work, iykyk:

use Mojo::Cache;

my $cache = Mojo::Cache->new(max_keys => 50);
$cache->set(foo => 'bar');
my $foo = $cache->get('foo');

Literally three lines and you're caching. Obsessed 🌸


πŸ’– DESCRIPTION πŸ’–

So Mojo::Cache is a naive in-memory cache β€” it keeps stuff in memory so you don't gotta re-do the work β€” and it comes with size limits so it doesn't just eat all your RAM like some kind of chaos demon 😭 It's simple, it's small, it slays.


⭐ ATTRIBUTES ⭐

Mojo::Cache implements these following attributes, no cap:

πŸ“ max_keys

my $max = $cache->max_keys;
$cache  = $cache->max_keys(50);

This is the maximum number of cache keys, and it defaults to 100 β€” that's the number, that's the default, don't forget it πŸ’œ

⚠️ Heads up bestie: setting the value to 0 will disable caching. Like, entirely. Zero keys allowed. She's off duty πŸ™ˆ So don't set it to 0 and then act surprised, fr fr.

🌈 METHODS 🌈

Mojo::Cache inherits all methods from Mojo::Base and implements these following new ones πŸ‘‘

🧚 get

my $value = $cache->get('foo');

Gets you the cached value. That's it. That's the whole method. Iconic brevity πŸŽ€

πŸ”₯ set

$cache = $cache->set(foo => 'bar');

Sets the cached value. Also returns the cache back to you so you can chain, main character energy ✨


πŸ‘‘ SEE ALSO πŸ‘‘

Go be nosy, these are all iconic:


And that's the page! πŸ’– This lil beauty is from perl v5.42.0 and dated 2026-07-15 β€” so go forth, cache things, keep max_keys off 0, and slay the day, bestie πŸ¦‹πŸ’…πŸŒŸ

πŸ“¦ where this page comes from