Cache::BaseCache(3pm)

๐Ÿ’– Cache::BaseCache โ€” the manual, but make it cute ๐Ÿ’–

okay bestie, welcome to Cache::BaseCache(3pm) from the User Contributed Perl Documentation ๐Ÿ’… this is the โœจ abstract cache base class โœจ and she's giving blueprint energy, not do-the-thing energy. sit down, hydrate, let's go ๐Ÿ“


๐ŸŒธ NAME ๐ŸŒธ

Cache::BaseCache โ€” abstract cache base class. yes that's literally it, she's abstract, she's a base, she's a class. icon behavior ๐Ÿ‘‘


โœจ DESCRIPTION โœจ

so <pink>BaseCache</pink> brings the functionality that every single instance of a cache has in common. all of them. every last one ๐Ÿซถ

the way it differs from the CacheUtils package is โ€” and this is the key part, iykyk โ€” it's designed to be used as a superclass for cache implementations. like, that's her whole purpose. she's the mom of the cache family, and she's not doing the caching herself, she's just handing down the genetics ๐Ÿงฌ๐Ÿ’–


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

Cache::BaseCache is meant to be used as a superclass for cache implementations. periodt.

the most effective way to use BaseCache is to use the protected _set_backend method โ€” that's the one that gets used to retrieve the persistence mechanism. once you've done that, your subclass can then inherit BaseCache's implementation of get, set, etc. slay, free real estate โœจ

but โ€” and listen, this is the plot twist ๐Ÿ˜ญ โ€” because inheriting static methods in Perl is genuinely difficult, your subclass will likely need to explicitly implement Clear, Purge, and Size. no shortcuts there, bestie, you're writing those yourself.

also, a factory pattern should be used to invoke the _complete_initialization routine after the object is constructed. that's the move. that's the ritual. do not skip the ritual ๐Ÿ”ฎ

here's the whole vibe in code, verbatim, because I would never dream of touching a single character:

package Cache::MyCache;

use vars qw( @ISA );
use Cache::BaseCache;
use Cache::MyBackend;

@ISA = qw( Cache::BaseCache );

sub new
{
  my ( $self ) = _new( @_ );

  $self->_complete_initialization( );

  return $self;
}

sub _new
{
  my ( $proto, $p_options_hash_ref ) = @_;
  my $class = ref( $proto ) || $proto;
  my $self = $class->SUPER::_new( $p_options_hash_ref );
  $self->_set_backend( new Cache::MyBackend( ) );
  return $self;
}

sub Clear
{
  foreach my $namespace ( _Namespaces( ) )
  {
    _Get_Backend( )->delete_namespace( $namespace );
  }
}

sub Purge
{
  foreach my $namespace ( _Namespaces( ) )
  {
    _Get_Cache( $namespace )->purge( );
  }
}

sub Size
{
  my $size = 0;

  foreach my $namespace ( _Namespaces( ) )
  {
    $size += _Get_Cache( $namespace )->size( );
  }

  return $size;
}

see how Clear, Purge, and Size are all just right there, hand-written by the subclass? yeah. that's the static-method tax. she takes her cut ๐Ÿ’€


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

go read these next, bestie, they're the rest of the friend group:

  • Cache::Cache
  • Cache::FileCache
  • Cache::MemoryCache

๐Ÿซถ AUTHOR ๐Ÿซถ

  • Original author: DeWitt Clinton <dewitt@unto.net>
  • Last author: $Author: dclinton $
  • Copyright (C) 2001-2003 DeWitt Clinton

no cap, that's the whole page. every method, every @ISA, every foreach, all preserved exactly. I would never gatekeep a _set_backend from you ๐Ÿ’œ

and that's a wrap, my love โ€” this little masterpiece came to you courtesy of perl v5.34.0, dated 2022-06-30, and it's filed under Cache::BaseCache(3pm). go forth and subclass responsibly, mwah ๐Ÿ˜˜๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from