summaryrefslogtreecommitdiff
path: root/redis/connection.py
diff options
context:
space:
mode:
authorAlex Grönholm <alex.gronholm+git@nextday.fi>2012-08-07 02:35:06 +0300
committerAlex Grönholm <alex.gronholm+git@nextday.fi>2012-08-07 02:35:06 +0300
commitb846290e8f822550d0c5f11e741d9ae10e3027e2 (patch)
tree5eea07b4ba7e904218f5ca4faec8c150b571e0f3 /redis/connection.py
parente16cd9cbcb1eac1c078b79a1e81fb70aec96dc6b (diff)
downloadredis-py-b846290e8f822550d0c5f11e741d9ae10e3027e2.tar.gz
Optimized code by caching certain bytestring literals
Diffstat (limited to 'redis/connection.py')
-rw-r--r--redis/connection.py25
1 files changed, 14 insertions, 11 deletions
diff --git a/redis/connection.py b/redis/connection.py
index 3574173..a6a8caf 100644
--- a/redis/connection.py
+++ b/redis/connection.py
@@ -20,6 +20,12 @@ except ImportError:
hiredis_available = False
+SYM_STAR = b('*')
+SYM_DOLLAR = b('$')
+SYM_CRLF = b('\r\n')
+SYM_LF = b('\n')
+
+
class PythonParser(object):
"Plain Python parsing class"
MAX_READ_LENGTH = 1000000
@@ -162,7 +168,7 @@ class HiredisParser(object):
self._reader.feed(buffer)
# proactively, but not conclusively, check if more data is in the
# buffer. if the data received doesn't end with \n, there's more.
- if not buffer.endswith(b('\n')):
+ if not buffer.endswith(SYM_LF):
continue
response = self._reader.gets()
return response
@@ -300,17 +306,14 @@ class Connection(object):
def pack_command(self, *args):
"Pack a series of arguments into a value Redis command"
- output = BytesIO()
- output.write(b('*'))
- output.write(b(str(len(args))))
- output.write(b('\r\n'))
+ output = SYM_STAR + b(str(len(args))) + SYM_CRLF
for enc_value in imap(self.encode, args):
- output.write(b('$'))
- output.write(b(str(len(enc_value))))
- output.write(b('\r\n'))
- output.write(enc_value)
- output.write(b('\r\n'))
- return output.getvalue()
+ output += SYM_DOLLAR
+ output += b(str(len(enc_value)))
+ output += SYM_CRLF
+ output += enc_value
+ output += SYM_CRLF
+ return output
class UnixDomainSocketConnection(Connection):