Last active
August 29, 2015 13:56
-
-
Save avar/9249160 to your computer and use it in GitHub Desktop.
Test the Redis SCAN command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Redis; | |
my $what = $ARGV[0] || "create"; | |
my $r = Redis->new(server => "127.0.0.1:6379"); | |
my $num_keys = 100_000; | |
if ($what =~ /\bflushall\b/) { | |
$r->flushall; | |
} | |
if ($what =~ /\bcreate\b/) { | |
spew("START Setting $num_keys test keys in Redis"); | |
for my $i (1..$num_keys) { | |
$r->hmset("test:key:$$:$i" => ( | |
value => $i, | |
time => time(), | |
)); | |
} | |
spew("DONE Setting $num_keys test keys in Redis"); | |
} | |
if ($what =~ /\biter\b/) { | |
spew("START Iterating over all keys"); | |
my $num_found_keys = 0; | |
my $num_keys_deleted = 0; | |
my $num_keys_replicated = 0; | |
my $cursor = 0; | |
do { | |
($cursor, my $keys) = $r->scan($cursor); | |
$num_found_keys += @$keys; | |
KEY: for my $key (grep /^test:key/, @$keys) { | |
my $v = $r->hget($key, "time"); | |
if ($what =~ /\breplicate\b/ and $v % 10 == 0) { | |
my %hash = $r->hgetall($key); | |
$r->del($key); | |
$num_keys_replicated++; | |
} | |
unless ($v) { | |
warn "Did key $key expire already?"; | |
next KEY; | |
} | |
if ($what =~ /\bdel\b/) { | |
$r->del($key); | |
$num_keys_deleted++; | |
} | |
} | |
} until $cursor == 0; | |
spew("DONE Iterating over all keys, found $num_found_keys of them and deleted $num_keys_deleted and replicated $num_keys_replicated"); | |
} | |
sub spew { | |
print STDERR localtime . " -- " . $_[0] . "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment