2014-12-20 09:17:59 +01:00
|
|
|
package Qpsmtpd::DB::Redis;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use parent 'Qpsmtpd::DB';
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my ( $class, %args ) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->name( delete $args{name} ) if defined $args{name};
|
|
|
|
$self->{redis_args} = {%args};
|
2014-12-30 17:13:37 +01:00
|
|
|
$self->init_db();
|
2014-12-20 09:17:59 +01:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2014-12-30 17:13:37 +01:00
|
|
|
sub init_redis {
|
2014-12-20 09:17:59 +01:00
|
|
|
my ( $self ) = @_;
|
2014-12-30 22:10:32 +01:00
|
|
|
# Stringy eval needed to allow 'use Qpmstpd::DB::Redis' to succeed
|
|
|
|
# even when Redis module is unavailable; mainly for testing
|
|
|
|
eval 'use Redis';
|
|
|
|
die $@ if $@;
|
2014-12-30 17:13:37 +01:00
|
|
|
my $redis = $self->{redis} = MyRedis->new( %{ $self->{redis_args} } );
|
2014-12-20 09:17:59 +01:00
|
|
|
$redis->selected(0);
|
|
|
|
return $redis;
|
|
|
|
}
|
|
|
|
|
2014-12-30 17:13:37 +01:00
|
|
|
sub init_db {
|
2014-12-20 09:17:59 +01:00
|
|
|
my ( $self ) = @_;
|
2014-12-30 17:13:37 +01:00
|
|
|
my $redis = $self->init_redis;
|
|
|
|
return if $redis->get('___smtpd_reserved___');
|
|
|
|
die "Redis DB at index 0 is already populated!" if $redis->dbsize;
|
2014-12-20 09:17:59 +01:00
|
|
|
$redis->set( ___smtpd_reserved___ => 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub redis {
|
2014-12-30 17:13:37 +01:00
|
|
|
my ( $self, $index ) = @_;
|
2014-12-20 09:17:59 +01:00
|
|
|
my $redis = $self->{redis} or die "redis(): redis was not initialized";
|
2014-12-30 17:13:37 +01:00
|
|
|
$index = $self->index if ! defined $index;
|
|
|
|
$redis->select( $index );
|
2014-12-20 09:17:59 +01:00
|
|
|
return $redis;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub index {
|
|
|
|
# Get index of database where the current plugin's data should be stored
|
|
|
|
my ( $self ) = @_;
|
|
|
|
return $self->{index} if $self->{index};
|
2014-12-30 17:13:37 +01:00
|
|
|
my $redis = $self->redis(0);
|
|
|
|
my %stores = $redis->hgetall('smtpd_stores');
|
2014-12-20 09:17:59 +01:00
|
|
|
return $self->{index} = $stores{ $self->name } if $stores{ $self->name };
|
|
|
|
my %rstores = reverse %stores;
|
|
|
|
for my $index ( 1 .. 255 ) {
|
|
|
|
$redis->select($index);
|
|
|
|
|
|
|
|
# This index is earmarked for something else
|
|
|
|
next if exists $rstores{$index};
|
|
|
|
|
|
|
|
# This index is populated by something else
|
|
|
|
next if $redis->dbsize;
|
|
|
|
|
|
|
|
# We can populate this empty store
|
2014-12-30 17:13:37 +01:00
|
|
|
$self->redis(0)->hset( 'smtpd_stores', $self->name => $index );
|
2014-12-20 09:17:59 +01:00
|
|
|
return $self->{index} = $index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get {
|
|
|
|
my ( $self, $key ) = @_;
|
|
|
|
if ( ! $key ) {
|
|
|
|
warn "No key provided, get() failed\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $self->redis->get($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub set {
|
|
|
|
my ( $self, $key, $val ) = @_;
|
|
|
|
if ( ! $key ) {
|
|
|
|
warn "No key provided, set() failed\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $self->redis->set( $key, $val );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub delete {
|
|
|
|
my ( $self, $key ) = @_;
|
|
|
|
if ( ! $key ) {
|
|
|
|
warn "No key provided, delete() failed\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $self->redis->del($key);
|
|
|
|
}
|
|
|
|
|
2014-12-30 17:13:37 +01:00
|
|
|
sub get_keys { shift->redis->keys('*') }
|
|
|
|
sub size { shift->redis->dbsize }
|
|
|
|
sub flush { shift->redis->flushdb }
|
2014-12-26 07:52:17 +01:00
|
|
|
|
2014-12-20 09:17:59 +01:00
|
|
|
package MyRedis;
|
|
|
|
eval "use parent 'Redis'";
|
|
|
|
|
|
|
|
# With all the (necessary) redundant select() going on, let's track the
|
|
|
|
# currently selected db and avoid the round trip when select() is a noop
|
|
|
|
|
|
|
|
sub select {
|
|
|
|
my $self = shift;
|
|
|
|
my ( $index ) = @_;
|
|
|
|
return if $index == $self->selected;
|
|
|
|
my $r = $self->SUPER::select(@_);
|
|
|
|
$self->selected( $index );
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub selected {
|
|
|
|
my ( $self, $index ) = @_;
|
|
|
|
$self->{selected} = $index if defined $index;
|
|
|
|
return $self->{selected} if defined $self->{selected};
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|