Skip to content

Instantly share code, notes, and snippets.

@gmaurice
Created September 29, 2010 21:34
Show Gist options
  • Save gmaurice/603611 to your computer and use it in GitHub Desktop.
Save gmaurice/603611 to your computer and use it in GitHub Desktop.
use lib ('lib');
use YAML::Syck;
use JSON;
use Redis;
use Storage; # My own module
use Storage::Riak; # My own too
my $storage = Storage->new(
driver => Storage::Riak->new);
my $redis = Redis->new;
my $context = $ARGV[0];
my %context_contacts = map { $_->{id} => 1 } @{ $storage->get("people/$context")->data->{contacts}->{data} };
warn "Finished riak request";
my %affinities = $redis->hgetall("Affinities/$context");
my (%affinity_inside, %affinity_outside);
warn "Finish redis requests";
$redis->quit;
foreach my $id (keys %affinities){
if ( $id ne $context){
if ( defined $context_contacts{$id} ){
$affinity_inside{$id} = $affinities{$id};
}else{
$affinity_outside{$id} = $affinities{$id};
}
}
}
my @contacts_sorted = sort { $affinity_inside{$b} <=> $affinity_inside{$a} } keys %affinity_inside;
my @person_id_sorted = sort { $affinity_outside{$b} <=> $affinity_outside{$a} } keys %affinity_outside;
my $output_outside;
my $output_inside;
foreach my $contact_id (@contacts_sorted[0..4]) {
$output_inside->{$contact_id}->{score} = $affinity_inside{$contact_id};
$output_inside->{$contact_id}->{is_contact} = $context_contacts{$contact_id} ? 1 : 0 ;
}
foreach my $person_id (@person_id_sorted[0..4]) {
$output_outside->{$person_id}->{score} = $affinity_outside{$person_id};
$output_outside->{$person_id}->{is_contact} = $context_contacts{$person_id} ? 1 : 0 ;
}
print encode_json($output_inside);
print encode_json($output_outside);
use strict;
use lib ('lib');
use Redis;
use JSON;
$| = 1;
my $start;
my $redis = Redis->new;
my $hash = "test-redis2.0-hash";
## Populate
$start = time;
print "Populating...";
for (my $i = 0 ; $i < 200000 ; $i++){
$redis->hset( $hash, 100000000000+int(rand(100000000000)) , int(rand (1000)));
}
print "done (". (time() - $start) ." secs)\n";
$start = time;
print "Quering redis hash...";
my %affinity = $redis->hgetall($hash);
print "done (". (time() - $start) ." secs)\n";
$start = time;
print "Sorting data...";
my @contacts_sorted = sort { $affinity{$b} <=> $affinity{$a} } keys %affinity;
my $output;
foreach my $contact_id (@contacts_sorted[0..4]) {
$output->{$contact_id} = $affinity{$contact_id};
}
print "done (". (time() - $start) ." secs)\n";
print encode_json($output);
$redis->del($hash);
$redis->quit;
I'm using Redis server 2.0.1.
With gmaurice/perl-Redis/redis2.0-test
Populating...done (28 secs)
Quering redis hash...done (6 secs)
Sorting data...done (2 secs)
{"111746758368":"999","130237392004":"999","155856033020":"999","131707929605":"999","176150911548":"999"}
With dpavlin/perl-Redis/redis2.0
Populating...done (33 secs)
Quering redis hash...done (20 secs)
Sorting data...done (4 secs)
{"194188414098":"999","127616607974":"999","166409902465":"999","193340046507":"999","137012404503":"999"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment