Exemple #1
0
    def _pack(self, *args):
        output = []
        # the client might have included 1 or more literal arguments in
        # the command name, e.g., 'CONFIG GET'. The Redis server expects these
        # arguments to be sent separately, so split the first argument
        # manually. All of these arguements get wrapped in the Token class
        # to prevent them from being encoded.
        command = args[0]
        if " " in command:
            args = tuple([Token(s) for s in command.split(" ")]) + args[1:]
        else:
            args = (Token(command),) + args[1:]

        buff = SYM_EMPTY.join((SYM_STAR, b(str(len(args))), SYM_CRLF))

        for arg in imap(self.encode, args):
            # to avoid large string mallocs, chunk the command into the
            # output list if we're sending large values
            if len(buff) > 6000 or len(arg) > 6000:
                buff = SYM_EMPTY.join((buff, SYM_DOLLAR, b(str(len(arg))), SYM_CRLF))
                output.append(buff)
                output.append(arg)
                buff = SYM_CRLF
            else:
                buff = SYM_EMPTY.join((buff, SYM_DOLLAR, b(str(len(arg))), SYM_CRLF, arg, SYM_CRLF))
        output.append(buff)
        return output
Exemple #2
0
    def encode(self, value):
        "Ported from redis-py"
        "Return a bytestring representation of the value"

        if isinstance(value, Token):
            return b(value.value)
        elif isinstance(value, bytes):
            return value
        elif isinstance(value, (int, long)):
            value = b(str(value))
        elif isinstance(value, float):
            value = b(repr(value))
        elif not isinstance(value, basestring):
            value = unicode(value)
        if isinstance(value, unicode):
            value = value.encode(self.charset, self.errors)
        return value
Exemple #3
0
 def __init__(self, db=None, password=None, charset="utf8", errors="strict"):
     self.charset = charset
     self.db = db if db is not None else 0
     self.password = password
     self.errors = errors
     self._buffer = b("")
     self._bulk_length = None
     self._disconnected = False
     # Format of _multi_bulk_stack elements is:
     # [[length-remaining, [replies] | None]]
     self._multi_bulk_stack = deque()
     self._request_queue = deque()
Exemple #4
0
@endcode

Redis google code project: http://code.google.com/p/redis/
Command doc strings taken from the CommandReference wiki page.

"""
from collections import deque

from twisted.internet import defer, protocol
from twisted.protocols import policies

from txredis import exceptions
from txredis._compat import b, imap, long, basestring, unicode


SYM_STAR = b("*")
SYM_DOLLAR = b("$")
SYM_CRLF = b("\r\n")
SYM_EMPTY = b("")


class Token(object):
    """
    Literal strings in Redis commands, such as the command names and any
    hard-coded arguments are wrapped in this class so we know not to apply
    and encoding rules on them.
    """

    def __init__(self, value):
        if isinstance(value, Token):
            value = value.value