Ok.  I just went through the CPAN code...

at least in Cache::Memcached, no socket connection is made until a get. in fact, they all seem to be made as needed, and not cached.

in any event, I use this code under MP2 , and do all my get/sets as

        MyApp::Functions::Memcached::memcached_set()
        MyApp::Functions::Memcached::memcached_get()

it centralizes all of my memcached operations, and i can turn it on/ off with a ENV var in startup.pl

=======================



package MyApp::Functions::Memcached;

######################################################

eval {
        require Cache::Memcached ;
        require Cache::Memcached::XS ;
};

######################################################

use constant USE_MEMCACHED=> $ENV{'USE_MEMCACHED'} ;
use constant USE_MEMCACHED_XS=> $ENV{'USE_MEMCACHED_XS'} ;

######################################################

my $_memd;
sub _setup_memcached {
        eval {
                if ( USE_MEMCACHED_XS )
                {
                        $_memd= Cache::Memcached::XS->new();
                }
                else
                {
                        $_memd= Cache::Memcached->new();
                }
                $_memd->set_compress_threshold( 10_000 );
                $_memd->set_servers(  [ '127.0.0.1:11211' ] );
        };
        if ( $@ )
        {
                print STDERR "\n MyApp::Functions::Memcached => Error 
Connecting";
                print STDERR "\n\t$@";
        }
}

sub get_use_memcached
{
        USE_MEMCACHED;
}

if ( USE_MEMCACHED ) {
        _setup_memcached();
}

sub memcached_set
{
        my      ( $key , $value , $exptime )= @_ ;
        if ( !USE_MEMCACHED || !$key )
        {
                return 0;
        }
        
        $exptime= $exptime || 6_000;
        if ( $exptime > 6_000 ) {
                $exptime= 6_000;
        }

        return $_memd->set( $key , $value , $exptime );
}

sub memcached_get
{
DEBUG_FUNCTION_NAME && & MyApp::Config::Debug::log_function_name ( undef, 'memcached_get' );
        my      ( $key ) = @_ ;
        if ( !USE_MEMCACHED )
        {
                return 0;
        }
        return $_memd->get( $key ) ;
}

sub memcached_getconnection
{
DEBUG_FUNCTION_NAME && & MyApp::Config::Debug::log_function_name ( undef, 'memcached_getconnection' );
        if ( !USE_MEMCACHED )
        {
                return 0;
        }
        return $_memd;
}


1;

Reply via email to