Cache::Cache(3pm)
π Cache::Cache β the Cache interface π
ok so bestie, you're looking at Cache::Cache(3pm), from the User Contributed Perl Documentation crew, and honestly? it's giving foundational. let me put you on. π«Ά
β¨ NAME β¨
Cache::Cache β the Cache interface. that's it, that's the whole name. iconic brevity. π
πΈ DESCRIPTION πΈ
so the Cache modules are literally designed to help a developer persist data for a specified period of time. that's the vibe. no cap.
bestie, people mostly use these in web applications to store data locally so they can skip all those repeated and redundant expensive calls to remote machines or databases. like why would you keep hitting the database when you could just... not? π
and people have also been known to use Cache::Cache for its straightforward interface when they wanna share data between runs of an application, or between invocations of a CGI-style script, or just as an easy-to-use abstraction of the filesystem or shared memory. so many options, all of them valid.
the Cache::Cache interface is implemented by classes that support the get, set, remove, size, purge, and clear instance methods, plus their corresponding static methods, all for persisting data across method calls. periodt.
π¦ CACHE::CACHE VERSUS CHI π¦
okay so real talk, Cache::Cache is in wide use and very stable, but it has not changed in years and is no longer actively developed. it's in its retirement era and we respect it. π
CHI is the successor to Cache::Cache. it adheres to the basic Cache::Cache API but adds new features and drivers (e.g. FastMmap and Memcached), improves performance, and addresses limitations in the Cache::Cache implementation. the authors recommend the use of CHI going forward. so like... yeah. moving on up. π₯
questions about Cache::Cache and CHI may be directed to the perl-cache mailing list at http://groups.google.com/group/perl-cache-discuss. they're friendly, go say hi. π
π USAGE π
first step, bestie: choose the best type of cache implementation for your needs. it's a whole vibe check.
the simplest cache is the MemoryCache, which is suitable for applications that are serving multiple sequential requests and wish to avoid making redundant expensive queries β like an Apache/mod_perl application talking to a database. classic.
if you wish to share that data between processes, then perhaps the SharedMemoryCache is appropriate, although its behavior is tightly bound to the underlying IPC mechanism, which varies from system to system, and it is unsuitable for large objects or large numbers of objects. so, know your limits, girl.
when the SharedMemoryCache is not acceptable, then FileCache offers all of the same functionality with similar performance metrics, and it is not limited in terms of the number of objects or their size. slay.
if you wish to maintain a strict limit on the size of a file system based cache, then the SizeAwareFileCache is the way to go. similarly, the SizeAwareMemoryCache and the SizeAwareSharedMemoryCache add size management functionality to the MemoryCache and SharedMemoryCache classes respectively. size-aware, self-aware, we love a queen who knows her bounds. π
using a cache is simple. here is some sample code for instantiating and using a file system based cache:
use Cache::FileCache;
my $cache = new Cache::FileCache( );
my $customer = $cache->get( $name );
if ( not defined $customer )
{
$customer = get_customer_from_db( $name );
$cache->set( $name, $customer, "10 minutes" );
}
return $customer;
π CONSTANTS π
$EXPIRES_NEVERβ the item being set in the cache will never expire. it's forever, fr fr. π$EXPIRES_NOWβ the item being set in the cache will expire immediately. gone. bye. π
π METHODS π
Clear( )β remove all objects from all caches of this type. the big sweep. β¨Purge( )β remove all objects that have expired from all caches of this type. expired stuff only, no extra casualties.Size( )β returns the total size of all objects in all caches of this type. numbers, bestie.new( $options_hash_ref )β construct a new instance of aCache::Cache.$options_hash_refis a reference to a hash containing configuration options; see the section OPTIONS below. πclear( )β remove all objects from the namespace associated with this cache instance.get( $key )β returns the data associated with$key.get_object( $key )β returns the underlyingCache::Objectobject used to store the cached data associated with$key. this will not trigger a removal of the cached object even if the object has expired. sneaky but on purpose.purge( )β remove all objects that have expired from the namespace associated with this cache instance.remove( $key )β delete the data associated with the$keyfrom the cache.set( $key, $data, [$expires_in] )β associates$datawith$keyin the cache.$expires_inindicates the time in seconds until this data should be erased, or the constant$EXPIRES_NOW, or the constant$EXPIRES_NEVER. defaults to$EXPIRES_NEVER. this variable can also be in the extended format of"[number] [unit]", e.g.,"10 minutes". the valid units ares,second,seconds,sec,m,minute,minutes,min,h,hour,hours,d,day,days,w,week,weeks,M,month,months,y,year, andyears. additionally,$EXPIRES_NOWcan be represented as"now"and$EXPIRES_NEVERcan be represented as"never". yes, all of those, memorize them, we're not skipping any. πset_object( $key, $object )β associates$keywithCache::Object$object. usingset_object(as opposed toset) does not trigger an automatic removal of expired objects.size( )β returns the total size of all objects in the namespace associated with this cache instance.get_namespaces( )β returns all the namespaces associated with this type of cache.
πΈ OPTIONS πΈ
the options are set by passing in a reference to a hash containing any of the following keys:
namespaceβ the namespace associated with this cache. defaults to"Default"if not explicitly set.default_expires_inβ the default expiration time for objects place in the cache. defaults to$EXPIRES_NEVERif not explicitly set.auto_purge_intervalβ sets the auto purge interval. if this option is set to a particular time ( in the same format as theexpires_in), then thepurge( )routine will be called during the firstsetafter the interval expires. the interval will then be reset. πauto_purge_on_setβ if this option is true, then the auto purge interval routine will be checked on everyset.auto_purge_on_getβ if this option is true, then the auto purge interval routine will be checked on everyget.
π¦ PROPERTIES π¦
(get|set)_namespace( )β the namespace of this cache instance.get_default_expires_in( )β the default expiration time for objects placed in this cache instance.get_keys( )β the list of keys specifying objects in the namespace associated with this cache instance.get_identifiers( )β this method has been deprecated in favor ofget_keys( ). π¨ old, retired, do not use, useget_keysinstead, bestie.(get|set)_auto_purge_interval( )β accesses the auto purge interval. if this option is set to a particular time ( in the same format as theexpires_in), then thepurge( )routine will be called during the firstgetafter the interval expires. the interval will then be reset.(get|set)_auto_purge_on_set( )β if this property is true, then the auto purge interval routine will be checked on everyset.(get|set)_auto_purge_on_get( )β if this property is true, then the auto purge interval routine will be checked on everyget.
π SEE ALSO π
CHI- the successor toCache::CacheCache::Object,Cache::MemoryCache,Cache::FileCache,Cache::SharedMemoryCache, andCache::SizeAwareFileCache
π«Ά AUTHOR π«Ά
- original author: DeWitt Clinton
<dewitt@unto.net> - last author:
$Author: dclinton $ - copyright (C) 2001-2003 DeWitt Clinton
and that's the whole page, bestie!! π this was Cache::Cache(3pm), rendered up all pretty from perl v5.34.0, dated 2022-06-30. go forth and cache responsibly. mwah. πβ¨ππ¦
π¦ where this page comes from
- debian bookworm: libcache-cache-perl 1.08-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- debian trixie: libcache-cache-perl 1.08-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- ubuntu noble: libcache-cache-perl 1.08-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- ubuntu resolute: libcache-cache-perl 1.08-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- ubuntu stonking: libcache-cache-perl 1.08-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms