App::Cache(3pm)
π App::Cache β the manual, but make it slay π
okay bestie, so this is App::Cache(3pm) from the User Contributed Perl Documentation section, and honestly? it's giving "let me remember that for you so you don't have to do it twice" and I'm obsessed. β¨
πΈ NAME πΈ
App::Cache β Easy application-level caching. that's it, that's the whole vibe. no cap. π
π SYNOPSIS π
here's the whole thing in action, copy-paste ready because I love you:
# in your class:
my $cache = App::Cache->new({ ttl => 60*60 });
$cache->delete('test');
my $data = $cache->get('test');
my $code = $cache->get_code("code", sub { $self->calculate() });
my $html = $cache->get_url("http://www.google.com/");
$cache->set('test', 'one');
$cache->set('test', { foo => 'bar' });
my $scratch = $cache->scratch;
$cache->clear;
that's the whole tour. constructor, delete, get, get_code, get_url, set, scratch, clear. iconic lineup fr fr π«Ά
π¦ DESCRIPTION π¦
so like. App::Cache lets your application cache data locally. and there are genuinely a few moments where you're gonna need that, bestie: when you're pulling info off the network, or when you've got a huge calculation to get through. nobody wants to do that twice. β¨
take Parse::BACKPAN::Packages, for example β it downloads a file off the net and parses it, building up a data structure. and only then can it actually give the programmer anything useful. rude, honestly. so Parse::BACKPAN::Packages uses App::Cache to cache both the file download and the data structures, which means when the data is cached it's much faster. ate. π
and where does it live? in the home directory of the user, inside a dot directory. so for example, the Parse::BACKPAN::Packages cache is actually stored underneath ~/.parse_backpan_packages/cache/. why? so that permissions are not a problem β it's a per-user, per-application cache. periodt. π
π METHODS π
π new π
the constructor creates an App::Cache object. it takes three optional parameters:
ttlβ holds the number of seconds until a cache entry expires. the default is 30 minutes.
my $cache = App::Cache->new({ ttl => 30*60 });
applicationβ sets the application name. if you're callingnew()from a class, the application gets automagically set to the calling class, so you should rarely need to pass it in:
my $cache = App::Cache->new({ application => 'Your::Module' });
directoryβ sets the directory used for the cache. normally it's just set for you, based on the application name, and created in the user's home directory. sometimes, for testing, it's useful to set it yourself:
my $cache = App::Cache->new({ directory => '/tmp/your/cache/dir' });
enabledβ can be set to0for testing, and in that case you will always get cache misses:
my $cache = App::Cache->new({ enabled => 0 });
π§Ή clear π§Ή
clears the cache. bye bye everything π«Ά
$cache->clear;
ποΈ delete ποΈ
deletes one entry in the cache:
$cache->delete('test');
π get π
gets an entry from the cache. returns undef if the entry does not exist or if it has expired:
my $data = $cache->get('test');
β¨ get_code β¨
this is a convenience method. it gets an entry from the cache, but if the entry does not exist, it sets the entry to the value of the code reference you passed in:
my $code = $cache->get_code("code", sub { $self->calculate() });
like, "get it if it's there, otherwise compute it and stash it." main character energy fr. π
π get_url π
this is a convenience method. it gets the content of a URL from the cache, but if the entry does not exist, it sets the entry to the content of the URL you passed in:
my $html = $cache->get_url("http://www.google.com/");
π scratch π
returns a directory in the cache that the application may use for scratch files:
my $scratch = $cache->scratch;
πΎ set πΎ
sets an entry in the cache. and ngl, an entry value may be an arbitrary Perl data structure, so go off:
$cache->set('test', 'one');
$cache->set('test', { foo => 'bar' });
π directory π
returns the full path to the cache directory. it's mainly useful when you're writing tests that use App::Cache and you want to clean up after yourself. and if that's what you're doing, you may want to explicitly set the 'application' constructor parameter β that way you avoid later cleaning up a cache dir that was already in use. π
my $dir = $cache->directory;
π AUTHOR π
Leon Brocard <acme@astray.com>
π COPYRIGHT π
Copyright (C) 2005-7, Leon Brocard
π©· LICENSE π©·
This module is free software; you can redistribute it or modify it under the same terms as Perl itself.
and that's the whole page bestie, no crumbs left. this one's from perl v5.34.0, dated 2022-06-26 β so go forth, cache ur stuff, and remember: the cache dir was in use first, so set that application param. mwah. ππβ¨π¦
π¦ where this page comes from
- debian bookworm: libapp-cache-perl 0.37-3 Β· Artistic-1.0-Perl OR GPL-1.0-only
- debian trixie: libapp-cache-perl 0.37-3 Β· Artistic-1.0-Perl OR GPL-1.0-only
- ubuntu noble: libapp-cache-perl 0.37-3 Β· Artistic-1.0-Perl OR GPL-1.0-only
- ubuntu resolute: libapp-cache-perl 0.37-3 Β· Artistic-1.0-Perl OR GPL-1.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms