return $result;
}
+sub _sock_result_bulk {
+ my $len = <$sock>;
+ warn "# len: ",dump($len);
+ return undef if $len eq "nil\r\n";
+ my $v;
+ read($sock, $v, $len) || die $!;
+ warn "# v: ",dump($v);
+ my $crlf;
+ read($sock, $crlf, 2); # skip cr/lf
+ return $v;
+}
+
=head1 Connection Handling
=head2 quit
sub get {
my ( $self, $k ) = @_;
print $sock "GET $k\r\n";
- my $len = <$sock>;
-# warn "# len: ",dump($len);
- return undef if $len eq "nil\r\n";
- my $v;
- read($sock, $v, $len) || die $!;
-# warn "# v: ",dump($v);
- my $crlf;
- read($sock, $crlf, 2); # skip cr/lf
- return $v;
+ _sock_result_bulk();
}
=head2 incr
sub type {
my ( $self, $key ) = @_;
- print $sock "type $key\r\n";
+ print $sock "TYPE $key\r\n";
_sock_result();
}
+=head1 Commands operating on the key space
+
+=head2 keys
+
+ my @keys = $r->keys( '*glob_pattern*' );
+
+=cut
+
+sub keys {
+ my ( $self, $glob ) = @_;
+ print $sock "KEYS $glob\r\n";
+ return split(/\s/, _sock_result_bulk());
+}
+
=head1 AUTHOR
Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
use warnings;
use strict;
-use Test::More tests => 44;
+use Test::More tests => 48;
use lib 'lib';
ok( $o->set('key-left' => $key_next), 'key-left' );
+my @keys;
+
foreach ( 0 .. $key_next ) {
- ok( $o->set( "key-$_" => $_ ), "set key-$_" );
- ok( $o->exists( "key-$_" ), "exists key-$_" );
- cmp_ok( $o->get( "key-$_" ), 'eq', $_, "get key-$_" );
+ my $key = 'key-' . $_;
+ push @keys, $key;
+ ok( $o->set( $key => $_ ), "set $key" );
+ ok( $o->exists( $key ), "exists $key" );
+ cmp_ok( $o->get( $key ), 'eq', $_, "get $key" );
cmp_ok( $o->incr( 'key-next' ), '==', $_ + 1, 'incr' );
cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $_ - 1, 'decr' );
}
cmp_ok( $o->decr('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' );
}
-ok( $o->del('key-next' ), 'del' );
+ok( $o->del( $_ ), "del $_" ) foreach map { "key-$_" } ( 'next', 'left' );
+ok( ! $o->del('non-existing' ), 'del non-existing' );
cmp_ok( $o->type('foo'), 'eq', 'string', 'type' );
+cmp_ok( $o->keys('key-*'), '==', $key_next + 1, 'key-*' );
+is_deeply( [ $o->keys('key-*') ], [ @keys ], 'keys' );
+
ok( $o->quit, 'quit' );