Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
finish connections commands
Browse files Browse the repository at this point in the history
  • Loading branch information
cofyc committed Sep 4, 2012
1 parent 352e5da commit 527cb8c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Commands
- sort

* fixbug: use recv on IO::Socket instead get
* complete method signatures
* support debug & timeout options
92 changes: 90 additions & 2 deletions lib/Redis.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ my &integer_reply_cb = { $_.Bool };
my &string_to_float_cb = { $_.Real };
my %command_callbacks = ();
%command_callbacks{"PING"} = { $_ eq "PONG" };
for "QUIT SET MSET PSETEX SETEX MIGRATE RENAME RENAMENX RESTORE HMSET".split(" ") -> $c {
for "AUTH QUIT SET MSET PSETEX SETEX MIGRATE RENAME RENAMENX RESTORE HMSET SELECT".split(" ") -> $c {
%command_callbacks{$c} = &status_code_reply_cb;
}
for "EXISTS SETNX EXPIRE EXPIREAT MOVE PERSIST PEXPIRE PEXPIREAT HSET HEXISTS HSETNX".split(" ") -> $c {
Expand Down Expand Up @@ -153,6 +153,16 @@ method !parse_response($response, $command) {
return $response;
}

####### Commands/Connection #######

method auth(Str $password) returns Bool {
return self!exec_command("AUTH", $password);
}

method echo($message) returns Str {
return self!exec_command("ECHO", $message);
}

# Ping the server.
method ping returns Bool {
return self!exec_command("PING");
Expand All @@ -163,6 +173,12 @@ method quit returns Bool {
return self!exec_command("QUIT");
}

method select(Int $index) returns Bool {
return self!exec_command("SELECT", $index);
}

####### Commands/Connection #######

###### Commands/Keys #######

method del(*@keys) returns Int {
Expand Down Expand Up @@ -238,7 +254,7 @@ method restore(Str $key, Int $milliseconds, Str $serialized-value) returns Bool
}

method sort(Str $key, Str :$by?,
Int :$offset?, Int :$count?,
Int :$offset?, Int :$count?,
:@get?,
Bool :$desc = False,
Bool :$alpha = False,
Expand Down Expand Up @@ -431,4 +447,76 @@ method hvals(Str $key) returns Array {

###### ! Commands/Hashes ######

###### Commands/Lists ######

method blpop(Int $timeout, *@keys) returns Any {
return self!exec_command("BLPOP", |@keys, $timeout);
}

method brpop(Int $timeout, *@keys) returns Any {
return self!exec_command("BRPOP", |@keys, $timeout);
}

method brpoplpush(Str $source, Str $destination, Int $timeout) returns Any {
return self!exec_command("BRPOPLPUSH", $source, $destination, $timeout);
}

method lindex(Str $key, Int $index) returns Any {
return self!exec_command("LINDEX", $key, $index);
}

method linsert(Str $key, Str $where where { $where eq any(["BEFORE", "AFTER"]) }, $pivot, $value) returns Any {
# TODO
}

method llen(Str $key) returns Int {
return self!exec_command("LLEN", $key);
}

method lpop(Str $key) returns Any {
return self!exec_command("LPOP", $key);
}

method lpush(Str $key, *@values) returns Int {
return self!exec_command("LPUSH", $key, |@values);
}

method lpushx(Str $key, *@values) returns Int {
return self!exec_command("LPUSHX", $key, |@values);
}

method lrange(Str $key, Int $start, Int $stop) returns Array {
return self!exec_command("LRANGE", $key, $start, $stop);
}

method lrem(Str $key, Int $count, $value) returns Int {
return self!exec_command("LREM", $key, $count, $value);
}

method lset(Str $key, Int $index, $value) {
return self!exec_command("LSET", $key, $index, $value);
}

method ltrim(Str $key, Int $start, Int $stop) {
return self!exec_command("LTRIM", $key, $start, $stop);
}

method rpop(Str $key) returns Any {
return self!exec_command("RPOP", $key);
}

method rpoplpush(Str $source, Str $destination) {
return self!exec_command("RPOPLPUSH", $source, $destination);
}

method rpush(Str $key, *@values) {
return self!exec_command("RPUSH", |@values);
}

method rpushx(Str $key, $value) {
return self!exec_command("RPUSHX", $value);
}

###### ! Commands/Lists ######

# vim: ft=perl6
15 changes: 13 additions & 2 deletions t/02-commands.t
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ is_deeply $r.rename("key", "newkey"), True;

# renamenx
{
my $failed = 1;
my $failed = 0;
try {
$r.renamenx("does_not_exists", "newkey");
CATCH {
Expand Down Expand Up @@ -192,8 +192,19 @@ is_deeply $r.hvals("hash"), ["value2", "21.1"];


###### Commands/Connection #######

{
my $failed = 0;
try {
$r.auth("WRONG PASSWORD");
CATCH {
default { $failed = 1 }
}
}
ok $failed;
}
is_deeply $r.echo("Hello World!"), "Hello World!";
is_deeply $r.ping, True;
is_deeply $r.select(2), True;
is_deeply $r.quit, True;

###### ! Commands/Connection #######

0 comments on commit 527cb8c

Please sign in to comment.