Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created November 30, 2011 03:37
Show Gist options
  • Save miyagawa/1407890 to your computer and use it in GitHub Desktop.
Save miyagawa/1407890 to your computer and use it in GitHub Desktop.
Redis get bits
use strict;
use AnyEvent::Redis;
my $r = AnyEvent::Redis->new(host => '127.0.0.1');
my $cv = AE::cv;
$r->del('foo', $cv);
$cv->recv;
for my $bit (1, 3, 4, 7, 8, 13, 16, 22, 24, 31, 32, 33) {
warn "SETBIT $bit 1\n";
my $cv = AE::cv;
$r->setbit('foo', $bit, 1, sub { $r->get('foo', $cv) });
my $v = $cv->recv;
warn unpack('B*', $v), "\n";
}
__END__
SETBIT 1 1
01000000
SETBIT 3 1
01010000
SETBIT 4 1
01011000
SETBIT 7 1
01011001
SETBIT 8 1
0101100110000000
SETBIT 13 1
0101100110000100
SETBIT 16 1
010110011000010010000000
SETBIT 22 1
010110011000010010000010
SETBIT 24 1
01011001100001001000001010000000
SETBIT 31 1
01011001100001001000001010000001
SETBIT 32 1
0101100110000100100000101000000110000000
SETBIT 33 1
0101100110000100100000101000000111000000
use strict;
use Redis;
my $r = Redis->new; # <- You need encoding => undef!
$r->del('foo');
for my $bit (1, 3, 4, 7, 8, 13, 16, 22, 24, 31, 32, 33) {
warn "SETBIT $bit 1\n";
$r->setbit('foo', $bit, 1);
warn unpack('B*', $r->get('foo')), "\n";
}
__END__
SETBIT 1 1
01000000
SETBIT 3 1
01010000
SETBIT 4 1
01011000
SETBIT 7 1
01011001
SETBIT 8 1
0101100111111101
SETBIT 13 1
0101100111111101
SETBIT 16 1
010110011111110111111101
SETBIT 22 1
010110011111110111111101
SETBIT 24 1
01011001111111011111110111111101
SETBIT 31 1
01011001111111011111110111111101
SETBIT 32 1
0101100111111101111111011111110111111101
SETBIT 33 1
0101100111111101111111011111110111111101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment