Memoiz...cached(3pm)
π Memoiz...cached(3pm) β User Contributed Perl Documentation π
okay bestie, sit down, grab a strawberry π, because we're about to talk about Memoize::Memcached, and honestly? it's giving "I made your slow function fast and I did it with style."
β¨ NAME β¨
Memoize::Memcached β use a memcached cache to memoize functions π
like, the whole vibe is: your function is expensive, memcached is fast, and this module is the matchmaker. no cap.
π¦ SYNOPSIS π¦
use Memoize::Memcached
memcached => {
servers => [ '127.0.0.1:11211' ],
};
memoize_memcached('foo');
# Function 'foo' is now memoized using the memcached server
# running on 127.0.0.1:11211 as the cache.
literally that's it. three lines and foo is living its best cached life β¨
π¨ WARNING π¨
The way flush_cache works with memcached can be dangerous. Please read the documentation below on flush_cache.
not me being dramatic but fr fr, READ THE flush_cache SECTION. it does more than you think it does. like, a lot more. π
π EXPORT π
This module exports memoize_memcached, flush_cache, and unmemoize. The unmemoize function is just the one from Memoize, and is made available for convenience.
so you get three besties at the party, and one of them (unmemoize) is literally just a re-gift from Memoize π«Ά
π FUNCTIONS π
memoize_memcached
This is the memcached equivalent of memoize. It works very similarly, except for some difference in options.
If the LIST_CACHE or SCALAR_CACHE options are passed in, memoize_memcached will complain and then pass the request along to memoize. The result will be a memoized function, but using whatever cache you specified and NOT using memcached at all.
like okay girl, if you bring your own cache we're not gonna fight you about it, we'll just hand you off to memoize and β¨ mind our business β¨
This function also accepts a memcached option, which expects a hashref. This is de-referenced and passed directly into an internal function which sets up the memcached configuration for that function. This contents of this hashref are mostly options passed to Cache::Memcached, with a few exceptions.
The actual key used to look up memoize data in memcached is formed from the function name, the normalized arguments, and some additional prefixes which can be set via the memcached option. These prefixes are key_prefix, list_key_prefix, and scalar_key_prefix.
The key_prefix defaults to memoize- if it's not passed in, or an undefined value is passed in.
The list_key_prefix and scalar_key_prefix options default to list- and scalar- respectively, by the same criteria.
So, the default way the key is generated is:
"memoize-<function>-list-<normalized args>"
or
"memoize-<function>-scalar-<normalized args>"
The function and normalized args portion of this key are set internally, but the memoize- prefix and the context portion can be configured with memcached options as follows:
"<key_prefix>-function-<list_key_prefix|scalar_key_prefix>-args"
Examples:
memoize_memcached('foo');
# keys generated will look like this:
# list context: memoize-foo-list-<argument signature>
# scalar context: memoize-foo-scalar-<argument signature>
memoize_memcached('foo',
memcached => {
servers => [ ... ],
key_prefix => '_M-',
list_key_prefix => 'L-',
scalar_key_prefix => 'S-',
},
;
# keys generated will look like this:
# list context: _M-foo-L-<argument signature>
# scalar context: _M-foo-S-<argument signature>
see how the prefixes just vibe-swap in? π _M-foo-L-<argument signature> β iconic, honestly.
flush_cache
The behavior documented in Memoize is sort of implemented. A call to flush_cache('memoized_function') will indeed clear the cache of all cached return values for that function, BUT it will also clear the entire memcached cache, including all other memoized functions using the same memcached cache, and even data unrelated to Memoize::Memcached in the same cache. It will flush the entire cache.
bestie. BESTIE. that's not a flush, that's a flood. it takes out the whole neighborhood, not just your house. π
There are 2 new ways to call this function:
flush_cache();
and
flush_cache(memoized_function => qw( an argument signature ));
The call without arguments will flush the entire memcached cache, just like the 1 argument version. This includes unrelated data. Be careful.
The call with 2 or more arguments will flush only the cached return values (array and scalar contexts) for a call to the function named by the first argument with an argument signature matching the second argument to the end. Unlike the other 2 ways to call this function, when called this way only the specified part of the cache is flushed.
I would recommended that only the 2 or more argument version of flush_cache be called unless you are very sure of what you are doing.
the 2+ argument version is the good one. it's the one with main character energy. the other two are the ones that take everyone down with them π
π GOTCHAS π
The biggest gotcha is that you probably never want to call flush_cache('memoized_function'). Because of the way CLEAR is implemented against memcached, this call will flush the entire memcached cache. Everything. Even stuff having nothing to do with Memoize::Memcached. You are warned.
EVERYTHING. not a vibe check, a vibe destruction. considered yourself warned fr fr π¨
π§ TO-DO π§
A more intuitive interface for handling different memcached server configurations would probably be useful.
(like yeah, lowkey, fair, that would slay β¨)
π AUTHOR π
David Trischuk, "<trischuk at gmail.com>"
π BUGS π
Please report any bugs or feature requests to "bug-memoize-memcached at rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Memoize-Memcached>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
the notification loop is real and it's actually kind of iconic π
πΈ SUPPORT πΈ
You can find documentation for this module with the perldoc command.
perldoc Memoize::Memcached
You can also look for information at:
- RT: CPAN's request tracker
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Memoize-Memcached>
- AnnoCPAN: Annotated CPAN documentation
<http://annocpan.org/dist/Memoize-Memcached>
- CPAN Ratings
<http://cpanratings.perl.org/d/Memoize-Memcached>
- Search CPAN
<http://search.cpan.org/dist/Memoize-Memcached>
four whole links, bestie, go read up π«Ά
β ACKNOWLEDGMENTS β
The tied hash portion of this module is heavily based on Cache::Memcached::Tie by Andrew Kostenko.
shoutout to Andrew, we love a foundation-layer king π
π COPYRIGHT & LICENSE π
Copyright 2008 David Trischuk, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page, bestie!! π this one came from perl v5.36.0, dated 2022-12-13, and it's the Memoiz...cached(3pm) page β go forth and cache responsibly (and please, for the love of everything, don't call flush_cache('memoized_function') π) periodt β¨π
π¦ where this page comes from
- debian bookworm: libmemoize-memcached-perl 0.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmemoize-memcached-perl 0.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmemoize-memcached-perl 0.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmemoize-memcached-perl 0.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmemoize-memcached-perl 0.04-2 Β· 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