use IO::Socket::INET;
use Data::Dump qw/dump/;
+use Carp qw/confess/;
=head1 NAME
=head2 set
- $r->set( foo => 'bar' );
+ $r->set( foo => 'bar', $new );
=cut
sub set {
- my ( $self, $k, $v ) = @_;
- print $sock "SET $k " . length($v) . "\r\n$v\r\n";
+ my ( $self, $k, $v, $new ) = @_;
+ print $sock ( $new ? "SETNX" : "SET" ) . " $k " . length($v) . "\r\n$v\r\n";
my $ok = <$sock>;
- die dump($ok) unless $ok eq "+OK\r\n";
+ confess dump($ok) unless $ok eq "+OK\r\n";
}
=head2 get
use warnings;
use strict;
-use Test::More tests => 93;
+use Test::More tests => 18;
use lib 'lib';
ok( $o->ping, 'ping' );
-ok( $o->set( foo => 'bar' ), 'set foo' );
-cmp_ok( $o->get( 'foo' ), 'eq', 'bar', 'get foo' );
+ok( $o->set( foo => 'bar' ), 'set foo => bar' );
+
+eval { $o->set( foo => 'bar', 1 ) };
+ok( $@, 'set foo => bar new again failed' );
+
+cmp_ok( $o->get( 'foo' ), 'eq', 'bar', 'get foo = bar' );
+
+ok( $o->set( foo => 'baz' ), 'set foo => baz' );
+
+cmp_ok( $o->get( 'foo' ), 'eq', 'baz', 'get foo = baz' );
ok( ! $o->get( 'non-existant' ), 'get non-existant' );
-foreach ( 0 .. 42 ) {
+foreach ( 0 .. 3 ) {
ok( $o->set( "key-$_" => $_ ), "set key-$_" );
cmp_ok( $o->get( "key-$_" ), 'eq', $_, "get key-$_" );
}