From: Dobrica Pavlinusic Date: Sat, 21 Mar 2009 22:38:56 +0000 (+0000) Subject: incr, incrby X-Git-Tag: 0.0801~56 X-Git-Url: http://git.rot13.org/?p=perl-Redis.git;a=commitdiff_plain;h=034e1cea13802c977bd220d8e393c4091d3a99bf;hp=43600e08dcbe970d69acf6a7fd1aa70d4d141fd3 incr, incrby git-svn-id: svn+ssh://llin/home/dpavlin/private/svn/Redis@7 447b33ff-793d-4489-8442-9bea7d161be5 --- diff --git a/lib/Redis.pm b/lib/Redis.pm index d03416e..952b80b 100644 --- a/lib/Redis.pm +++ b/lib/Redis.pm @@ -112,7 +112,24 @@ sub get { return $v; } +=head2 incr + $r->incr('counter'); + $r->incr('tripplets', 3); + +=cut + +sub incr { + my ( $self, $key, $value ) = @_; + if ( defined $value ) { + print $sock "INCRBY $key $value\r\n"; + } else { + print $sock "INCR $key\r\n"; + } + my $count = <$sock>; + warn "# $key = $count"; + return $count; +} =head1 AUTHOR diff --git a/t/01-Redis.t b/t/01-Redis.t index 1990e08..6d876f5 100755 --- a/t/01-Redis.t +++ b/t/01-Redis.t @@ -3,7 +3,7 @@ use warnings; use strict; -use Test::More tests => 18; +use Test::More tests => 28; use lib 'lib'; @@ -28,9 +28,21 @@ cmp_ok( $o->get( 'foo' ), 'eq', 'baz', 'get foo = baz' ); ok( ! $o->get( 'non-existant' ), 'get non-existant' ); -foreach ( 0 .. 3 ) { +ok( $o->set('key-next' => 0), 'key-next = 0' ); + +my $key_next = 3; + +foreach ( 0 .. $key_next ) { ok( $o->set( "key-$_" => $_ ), "set key-$_" ); cmp_ok( $o->get( "key-$_" ), 'eq', $_, "get key-$_" ); + cmp_ok( $o->incr( 'key-next' ), '==', $_ + 1, 'incr' ); +} + +cmp_ok( $o->get( 'key-next' ), '==', $key_next + 1, 'key-next' ); + +ok( $o->set('test-incrby', 0), 'test-incrby' ); +foreach ( 1 .. 3 ) { + cmp_ok( $o->incr('test-incrby', 3), '==', $_ * 3, 'incrby 3' ); } ok( $o->quit, 'quit' );