summaryrefslogtreecommitdiff
path: root/redis/client/debug.py
blob: 8241d8ee1d30a4a0869e9074fb7b98d565e4ed72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import logging
from redis.client.base import Connection, ConnectionPool, Redis, Pipeline

log = logging.getLogger("redis")

def repr_command(args):
    "Represents a command as a string."
    command = [args[0]]
    if len(args) > 1:
        command.extend(repr(x) for x in args[1:])
    return ' '.join(command)

class DebugConnection(Connection):
    def _connect(self, redis_instance):
        log.debug("connecting to %s:%d/%d", self.host, self.port, self.db)
        super(DebugConnection, self)._connect(redis_instance)

    def _disconnect(self):
        log.debug("disconnecting from %s:%d/%d", self.host, self.port, self.db)
        super(DebugConnection, self)._disconnect()


class DebugClient(Redis):
    def __init__(self, *args, **kwargs):
        pool = kwargs.pop('connection_pool', None)
        if not pool:
            pool = ConnectionPool(connection_class=DebugConnection)
        kwargs['connection_pool'] = pool
        super(DebugClient, self).__init__(*args, **kwargs)

    def _execute_command(self, command_name, command, **options):
        log.debug(repr_command(command))
        return super(DebugClient, self)._execute_command(
            command_name, command, **options
            )

    def pipeline(self, transaction=True):
        """
        Return a new pipeline object that can queue multiple commands for
        later execution. ``transaction`` indicates whether all commands
        should be executed atomically. Apart from multiple atomic operations,
        pipelines are useful for batch loading of data as they reduce the
        number of back and forth network operations between client and server.
        """
        return DebugPipeline(
            self.connection,
            transaction,
            self.encoding,
            self.errors
            )


class DebugPipeline(Pipeline):
    def _execute_transaction(self, commands):
        log.debug("MULTI")
        for command in commands:
            log.debug("TRANSACTION> "+ repr_command(command[1]))
        log.debug("EXEC")
        return super(DebugPipeline, self)._execute_transaction(commands)

    def _execute_pipeline(self, commands):
        for command in commands:
            log.debug("PIPELINE> " + repr_command(command[1]))
        return super(DebugPipeline, self)._execute_pipeline(commands)