Skip to content

Instantly share code, notes, and snippets.

@akzhan akzhan/Redis.pm
Last active Apr 18, 2017

Embed
What would you like to do?
Simple Redis client module
package YourCompany::Util::Redis;
use YourCompany::Perl;
use YourCompany::Config ();
BEGIN {
require parent;
eval {
parent->import( 'Redis::Fast' );
1;
} or do {
parent->import( 'Redis' );
};
}
my $config = YourCompany::Config->redis;
sub new {
my ( $class ) = @_;
my $index = $config->{index} // 0;
my $self = $class->SUPER::new(
server => "$config->{host}:$config->{port}",
reconnect => 1,
on_connect => sub {
my $self = shift;
$self->select( $index ) if $index != 0;
$self;
},
);
$self;
}
my ( $work_pid, $instance ) = ( 0, undef );
sub instance {
my ( $class ) = @_;
my $pid = $$;
if ( $work_pid != $pid ) {
$instance = undef;
$work_pid = $pid;
}
$instance //= $class->new;
return $instance;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.