App::Cache(3pm)

πŸ“¦
version 0.37 Β· 5 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute

πŸ’– 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 calling new() 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 to 0 for 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 (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