Skip to content

Commit 2dee8e9

Browse files
committed
encode commands at the last possible moment to make logging easier later
1 parent 518801e commit 2dee8e9

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

redis/client.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ def _execute_command(self, command_name, command, **options):
336336
if self.subscribed and not subscription_command:
337337
raise RedisError("Cannot issue commands other than SUBSCRIBE and "
338338
"UNSUBSCRIBE while channels are open")
339+
command = self._encode_command(command)
339340
try:
340341
self.connection.send(command, self)
341342
if subscription_command:
@@ -347,14 +348,17 @@ def _execute_command(self, command_name, command, **options):
347348
if subscription_command:
348349
return None
349350
return self.parse_response(command_name, **options)
351+
352+
def _encode_command(self, args):
353+
cmds = ['$%s\r\n%s\r\n' % (len(enc_value), enc_value)
354+
for enc_value in imap(self.encode, args)]
355+
return '*%s\r\n%s' % (len(cmds), ''.join(cmds))
350356

351357
def execute_command(self, *args, **options):
352358
"Sends the command to the redis server and returns it's response"
353-
cmds = ['$%s\r\n%s\r\n' % (len(enc_value), enc_value)
354-
for enc_value in imap(self.encode, args)]
355359
return self._execute_command(
356360
args[0],
357-
'*%s\r\n%s' % (len(cmds), ''.join(cmds)),
361+
args,
358362
**options
359363
)
360364

@@ -1426,10 +1430,10 @@ def _execute_command(self, command_name, command, **options):
14261430
def _execute_transaction(self, commands):
14271431
# wrap the commands in MULTI ... EXEC statements to indicate an
14281432
# atomic operation
1429-
all_cmds = ''.join([c for _1, c, _2 in chain(
1430-
(('', 'MULTI\r\n', ''),),
1433+
all_cmds = ''.join([self._encode_command(c) for _1, c, _2 in chain(
1434+
(('', ('MULTI',), ''),),
14311435
commands,
1432-
(('', 'EXEC\r\n', ''),)
1436+
(('', ('EXEC',), ''),)
14331437
)])
14341438
self.connection.send(all_cmds, self)
14351439
# parse off the response for MULTI and all commands prior to EXEC
@@ -1455,7 +1459,7 @@ def _execute_transaction(self, commands):
14551459

14561460
def _execute_pipeline(self, commands):
14571461
# build up all commands into a single request to increase network perf
1458-
all_cmds = ''.join([c for _1, c, _2 in commands])
1462+
all_cmds = ''.join([self._encode_command(c) for _1, c, _2 in commands])
14591463
self.connection.send(all_cmds, self)
14601464
data = []
14611465
for command_name, _, options in commands:

0 commit comments

Comments
 (0)