Singleton::Memcache is a perl wrapper around Cache::Memcached. (Which is itself a wrapper around Danga's Memcached providing a fast in-memory cache of arbitary objects.)

This singleton wrapper serves two purposes:

Updated: per comments from Tanktalus

# -*- cperl -*- # =head1 NAME Singleton::Memcache - A singleton wrapper around Danga's memcache. =head1 SYNOPSIS =for example begin #!/usr/bin/perl -w use Singleton::Memcache; use strict; my $cache = Singleton::Memcache->instance(); $cache->set( "bob", "your uncle" ); $cache->get( "bob" ); $cache->delete( "bob" ); =for example end =head1 DESCRIPTION Singleton Wrapper around Cache::Memcached This is a proxy object, which will attempt to create a Cache::Memcac +hed object to forward requests to. If we cannot create one we just use stub implementations to fail gracefully. We will fail if the <Cache::Memcached> Perl module isn't available o +r otherwise fails to connect. Steve -- www.steve.org.uk $Id: Memcache.pm,v 1.8 2005/11/09 02:30:08 steve Exp $ =cut package Singleton::Memcache; use strict; use warnings; # # The single, global, instance of this object # my $_cache; # # The memcache instance we use. # my $_memcache; =head2 new Gain access to the cache instance. If the singleton object has been created return it. Otherwise create a new instance. =cut sub instance { $_cache ||= (shift)->new(); } =head2 new Constructor. We connect to the cache, and store a reference to it internally. If the cache is disabled in the configuration file then we do nothin +g, similarly if the creation of the cache connection fails then we just quietly disable ourself. =cut sub new { my ( $self ) = (@_); my $class=ref($self) || $self; # # See if we're enabled # my $test = "use Cache::Memcached;"; # # Test loading the Cache module, if it fails then # the cache isn't enabled. # eval( $test ); if ( $@ ) { $enabled = 0; } # # Connect # if ( $enabled ) { $_memcache = new Cache::Memcached { 'servers' => [ "localhost:11211" ] }; } return bless {}, $class; } =head2 disconnect_all Disconnect from the cache =cut sub disconnect_all { my ( $self, @rest ) = ( @_ ); $_memcache->disconnect_all( @rest ) if defined( $_memcache ); } =head2 get Get a key from the cache =cut sub get { my ( $self, @rest ) = ( @_ ); $_memcache->get( @rest ) if defined( $_memcache ); } =head2 set Set a key in the cache. Note that we'll get a warning from Perl if we attempt to set an undefined value. We could catch it here, but we do not. =cut sub set { my ( $self, @rest ) = ( @_ ); $_memcache->set( @rest ) if defined( $_memcache ); } =head2 delete Delete a key from the cache. =cut sub delete { my ( $self, @rest ) = ( @_ ); $_memcache->delete( @rest ) if defined( $_memcache ); } 1;

In reply to A singleton wrapper around Cache::Memcached by skx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.