Pass multiple keys to del() to speed up prune_db()
This commit is contained in:
parent
1320a01f46
commit
b6311caae0
@ -159,17 +159,19 @@ sub size {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub delete {
|
sub delete {
|
||||||
my ( $self, $key ) = @_;
|
my ( $self, @keys ) = @_;
|
||||||
my $tied = $self->{tied};
|
my $tied = $self->{tied};
|
||||||
if ( ! $tied ) {
|
if ( ! $tied ) {
|
||||||
warn "DBM db not yet set up, delete() failed\n";
|
warn "DBM db not yet set up, delete() failed\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( ! $key ) {
|
if ( ! @keys ) {
|
||||||
warn "No key provided, delete() failed\n";
|
warn "No key provided, delete() failed\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
delete $tied->{$key};
|
@keys = grep { exists $tied->{$_} } @keys;
|
||||||
|
delete @$tied{@keys};
|
||||||
|
return scalar @keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub flush {
|
sub flush {
|
||||||
|
@ -91,12 +91,12 @@ sub set {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub delete {
|
sub delete {
|
||||||
my ( $self, $key ) = @_;
|
my ( $self, @keys ) = @_;
|
||||||
if ( ! $key ) {
|
if ( ! @keys ) {
|
||||||
warn "No key provided, delete() failed\n";
|
warn "No key provided, delete() failed\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return $self->redis->del($key);
|
return $self->redis->del(@keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get_keys { shift->redis->keys('*') }
|
sub get_keys { shift->redis->keys('*') }
|
||||||
|
@ -489,7 +489,7 @@ sub prune_db {
|
|||||||
$self->db->lock or return;
|
$self->db->lock or return;
|
||||||
my $count = $self->db->size;
|
my $count = $self->db->size;
|
||||||
|
|
||||||
my $pruned = 0;
|
my @to_delete;
|
||||||
my $greylist = {};
|
my $greylist = {};
|
||||||
my @keys = $self->db->get_keys or return;
|
my @keys = $self->db->get_keys or return;
|
||||||
@$greylist{ @keys } = ( $self->db->mget(@keys) );
|
@$greylist{ @keys } = ( $self->db->mget(@keys) );
|
||||||
@ -497,9 +497,10 @@ sub prune_db {
|
|||||||
my ($ts) = split /:/, delete $greylist->{$key};
|
my ($ts) = split /:/, delete $greylist->{$key};
|
||||||
my $age = $self->now - $ts;
|
my $age = $self->now - $ts;
|
||||||
next if $age < $self->{_args}{white_timeout};
|
next if $age < $self->{_args}{white_timeout};
|
||||||
$pruned++;
|
push @to_delete, $key;
|
||||||
$self->db->delete($key);
|
|
||||||
}
|
}
|
||||||
|
return if ! @to_delete;
|
||||||
|
my $pruned = $self->db->delete(@to_delete);
|
||||||
$self->db->unlock;
|
$self->db->unlock;
|
||||||
$self->log(LOGINFO, "pruned $pruned of $count DB entries");
|
$self->log(LOGINFO, "pruned $pruned of $count DB entries");
|
||||||
}
|
}
|
||||||
|
@ -56,8 +56,18 @@ sub __delete {
|
|||||||
$db->flush;
|
$db->flush;
|
||||||
$db->set( oink => 1 );
|
$db->set( oink => 1 );
|
||||||
$db->set( quack => 1 );
|
$db->set( quack => 1 );
|
||||||
$db->delete('quack');
|
$db->set( woof => 1 );
|
||||||
is( join( '|', $db->get_keys ), 'oink', 'delete() removes key' );
|
$db->set( moo => 1 );
|
||||||
|
is( $db->delete('quack'), 1,
|
||||||
|
'delete() return value when removing a single key' );
|
||||||
|
is( join( '|', sort $db->get_keys ), 'moo|oink|woof',
|
||||||
|
'delete() removes a single key' );
|
||||||
|
is( $db->delete(qw( moo oink )), 2,
|
||||||
|
'delete() return value when removing a single key' );
|
||||||
|
is( join( '|', sort $db->get_keys ), 'woof',
|
||||||
|
'delete() removes two keys' );
|
||||||
|
is( $db->delete('noop'), 0,
|
||||||
|
'delete() return value when removing a non-existent key' );
|
||||||
$db->unlock;
|
$db->unlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,8 +115,19 @@ sub __delete {
|
|||||||
$redis->flushdb;
|
$redis->flushdb;
|
||||||
$redis->set( oink => 1 );
|
$redis->set( oink => 1 );
|
||||||
$redis->set( quack => 1 );
|
$redis->set( quack => 1 );
|
||||||
$db->delete('quack');
|
$redis->set( woof => 1 );
|
||||||
is( join( '|', $redis->keys('*') ), 'oink', 'delete() removes key' );
|
$redis->set( moo => 1 );
|
||||||
|
|
||||||
|
is( $db->delete('quack'), 1,
|
||||||
|
'delete() return value when removing a single key' );
|
||||||
|
is( join( '|', sort $redis->keys('*') ), 'moo|oink|woof',
|
||||||
|
'delete() removes a single key' );
|
||||||
|
is( $db->delete(qw( moo oink )), 2,
|
||||||
|
'delete() return value when removing a single key' );
|
||||||
|
is( join( '|', sort $redis->keys('*') ), 'woof',
|
||||||
|
'delete() removes two keys' );
|
||||||
|
is( $db->delete('noop'), 0,
|
||||||
|
'delete() return value when removing a non-existent key' );
|
||||||
}
|
}
|
||||||
|
|
||||||
sub __get_keys {
|
sub __get_keys {
|
||||||
@ -158,7 +169,14 @@ sub select { $_[0]->{selected} = $_[1] }
|
|||||||
sub dbsize { scalar keys %{ $_[0]->fakestore } }
|
sub dbsize { scalar keys %{ $_[0]->fakestore } }
|
||||||
sub get { $_[0]->fakestore->{ $_[1] } }
|
sub get { $_[0]->fakestore->{ $_[1] } }
|
||||||
sub set { $_[0]->fakestore->{ $_[1] } = $_[2] }
|
sub set { $_[0]->fakestore->{ $_[1] } = $_[2] }
|
||||||
sub del { delete $_[0]->fakestore->{ $_[1] } }
|
|
||||||
|
sub del {
|
||||||
|
my ($self,@keys) = @_;
|
||||||
|
my $f = $self->fakestore;
|
||||||
|
@keys = grep { exists $f->{$_} } @keys;
|
||||||
|
delete @$f{ @keys };
|
||||||
|
return scalar @keys;
|
||||||
|
}
|
||||||
|
|
||||||
sub mget {
|
sub mget {
|
||||||
my ($self,@keys) = @_;
|
my ($self,@keys) = @_;
|
||||||
|
Loading…
Reference in New Issue
Block a user