Qpsmtpd::DB::Redis simplifications

This commit is contained in:
Jared Johnson 2014-12-30 10:13:37 -06:00
parent 11646b9a27
commit 80e4629f9d

View File

@ -1,49 +1,39 @@
package Qpsmtpd::DB::Redis; package Qpsmtpd::DB::Redis;
use strict; use strict;
use warnings; use warnings;
use Redis;
use parent 'Qpsmtpd::DB'; use parent 'Qpsmtpd::DB';
sub new { sub new {
my ( $class, %args ) = @_; my ( $class, %args ) = @_;
my $self = bless {}, $class; my $self = bless {}, $class;
eval 'use Redis';
die $@ if $@;
$self->name( delete $args{name} ) if defined $args{name}; $self->name( delete $args{name} ) if defined $args{name};
$self->{redis_args} = {%args}; $self->{redis_args} = {%args};
$self->init_internal_db() or return; $self->init_db();
return $self; return $self;
} }
sub redis_zero { sub init_redis {
# Internal redis db
my ( $self ) = @_; my ( $self ) = @_;
my $redis = $self->{redis} ||= $self->new_redis; my $redis = $self->{redis} = MyRedis->new( %{ $self->{redis_args} } );
$redis->select(0);
return $redis;
}
sub new_redis {
my ( $self ) = @_;
my $redis = $self->{redis} = MyRedis->new( %{ $self->{args} } );
$redis->selected(0); $redis->selected(0);
return $redis; return $redis;
} }
sub init_internal_db { sub init_db {
my ( $self ) = @_; my ( $self ) = @_;
my $redis = $self->redis_zero; my $redis = $self->init_redis;
return 1 if $redis->get('___smtpd_reserved___'); return if $redis->get('___smtpd_reserved___');
# Don't try to init a redis db already populated by something else die "Redis DB at index 0 is already populated!" if $redis->dbsize;
return 0 if $redis->dbsize;
$redis->set( ___smtpd_reserved___ => 1 ); $redis->set( ___smtpd_reserved___ => 1 );
return 1;
} }
sub redis { sub redis {
my ( $self ) = @_; my ( $self, $index ) = @_;
my $redis = $self->{redis} or die "redis(): redis was not initialized"; my $redis = $self->{redis} or die "redis(): redis was not initialized";
$redis->select( $self->index ); $index = $self->index if ! defined $index;
$redis->select( $index );
return $redis; return $redis;
} }
@ -51,8 +41,8 @@ sub index {
# Get index of database where the current plugin's data should be stored # Get index of database where the current plugin's data should be stored
my ( $self ) = @_; my ( $self ) = @_;
return $self->{index} if $self->{index}; return $self->{index} if $self->{index};
my $redis = $self->redis_zero; my $redis = $self->redis(0);
my %stores = $self->redis_zero->hgetall('smtpd_stores'); my %stores = $redis->hgetall('smtpd_stores');
return $self->{index} = $stores{ $self->name } if $stores{ $self->name }; return $self->{index} = $stores{ $self->name } if $stores{ $self->name };
my %rstores = reverse %stores; my %rstores = reverse %stores;
for my $index ( 1 .. 255 ) { for my $index ( 1 .. 255 ) {
@ -65,7 +55,7 @@ sub index {
next if $redis->dbsize; next if $redis->dbsize;
# We can populate this empty store # We can populate this empty store
$self->redis_zero->hset( 'smtpd_stores', $self->name => $index ); $self->redis(0)->hset( 'smtpd_stores', $self->name => $index );
return $self->{index} = $index; return $self->{index} = $index;
} }
} }
@ -88,16 +78,6 @@ sub set {
return $self->redis->set( $key, $val ); return $self->redis->set( $key, $val );
} }
sub get_keys {
my ( $self ) = @_;
return $self->redis->keys('*');
}
sub size {
my ( $self ) = @_;
return $self->redis->dbsize;
}
sub delete { sub delete {
my ( $self, $key ) = @_; my ( $self, $key ) = @_;
if ( ! $key ) { if ( ! $key ) {
@ -107,7 +87,9 @@ sub delete {
return $self->redis->del($key); return $self->redis->del($key);
} }
sub flush { shift->redis->flushdb } sub get_keys { shift->redis->keys('*') }
sub size { shift->redis->dbsize }
sub flush { shift->redis->flushdb }
package MyRedis; package MyRedis;
eval "use parent 'Redis'"; eval "use parent 'Redis'";