Skip to content

Commit 18b6cb7

Browse files
djanowskiantirez
authored andcommitted
Add DISCARD command to discard queued MULTI commands.
1 parent c8c7244 commit 18b6cb7

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

redis.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ static void zscoreCommand(redisClient *c);
653653
static void zremrangebyscoreCommand(redisClient *c);
654654
static void multiCommand(redisClient *c);
655655
static void execCommand(redisClient *c);
656+
static void discardCommand(redisClient *c);
656657
static void blpopCommand(redisClient *c);
657658
static void brpopCommand(redisClient *c);
658659
static void appendCommand(redisClient *c);
@@ -733,6 +734,7 @@ static struct redisCommand cmdTable[] = {
733734
{"type",typeCommand,2,REDIS_CMD_INLINE,1,1,1},
734735
{"multi",multiCommand,1,REDIS_CMD_INLINE,0,0,0},
735736
{"exec",execCommand,1,REDIS_CMD_INLINE,0,0,0},
737+
{"discard",discardCommand,1,REDIS_CMD_INLINE,0,0,0},
736738
{"sync",syncCommand,1,REDIS_CMD_INLINE,0,0,0},
737739
{"flushdb",flushdbCommand,1,REDIS_CMD_INLINE,0,0,0},
738740
{"flushall",flushallCommand,1,REDIS_CMD_INLINE,0,0,0},
@@ -2141,7 +2143,7 @@ static int processCommand(redisClient *c) {
21412143
}
21422144

21432145
/* Exec the command */
2144-
if (c->flags & REDIS_MULTI && cmd->proc != execCommand) {
2146+
if (c->flags & REDIS_MULTI && cmd->proc != execCommand && cmd->proc != discardCommand) {
21452147
queueMultiCommand(c,cmd);
21462148
addReply(c,shared.queued);
21472149
} else {
@@ -6051,6 +6053,18 @@ static void multiCommand(redisClient *c) {
60516053
addReply(c,shared.ok);
60526054
}
60536055

6056+
static void discardCommand(redisClient *c) {
6057+
if (!(c->flags & REDIS_MULTI)) {
6058+
addReplySds(c,sdsnew("-ERR DISCARD without MULTI\r\n"));
6059+
return;
6060+
}
6061+
6062+
freeClientMultiState(c);
6063+
initClientMultiState(c);
6064+
c->flags &= (~REDIS_MULTI);
6065+
addReply(c,shared.ok);
6066+
}
6067+
60546068
static void execCommand(redisClient *c) {
60556069
int j;
60566070
robj **orig_argv;

test-redis.tcl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,18 @@ proc main {server port} {
16631663
list $v1 $v2 $v3
16641664
} {QUEUED QUEUED {{a b c} PONG}}
16651665

1666+
test {DISCARD} {
1667+
$r del mylist
1668+
$r rpush mylist a
1669+
$r rpush mylist b
1670+
$r rpush mylist c
1671+
$r multi
1672+
set v1 [$r del mylist]
1673+
set v2 [$r discard]
1674+
set v3 [$r lrange mylist 0 -1]
1675+
list $v1 $v2 $v3
1676+
} {QUEUED OK {a b c}}
1677+
16661678
test {APPEND basics} {
16671679
list [$r append foo bar] [$r get foo] \
16681680
[$r append foo 100] [$r get foo]

0 commit comments

Comments
 (0)