| Next: Singleton::Session | Previous: Singleton::DBI | [Table of Contents] |
Singleton::Memcache - A singleton wrapper around Danga's memcache.
#!/usr/bin/perl -w
use Singleton::Memcache;
use strict;
# Get the cache handle.
my $cache = Singleton::Memcache->instance();
# Set a value in the cache, and retrieve it.
$cache->set( "bob", "your uncle" );
$cache->get( "bob" );
# Finally delete the key.
$cache->delete( "bob" );
This module implements a Singleton wrapper around Cache::Memcached,
which allows global access to a running memcached instance.
This singleton is a proxy object, which will attempt to create an
object to forward requests to, and silently swallow requests if this
creation fails.
We will fail if:
1. The configuration file says we're disabled.
2. The <Cache::Memcached> Perl module isn't enabled, or otherwise
fails to connect.
For more discussion on the code, and why it is implemented in this manner you might be interested in the following node at Perlmonks: http://www.perlmonks.org/?node_id=507753
Gain access to the cache instance. If the singleton object has been created return it. Otherwise create a new instance.
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 nothing, similarly if the creation of the cache connection fails then we just quietly disable ourself.
Disconnect from the cache
Get a key from the cache
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.
Return statistic information about the current server.
Delete a key from the cache.
Steve Kemp
http://www.steve.org.uk/
Copyright (c) 2005-2007 by Steve Kemp. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The LICENSE file contains the full text of the license.