File: mixin.py

package info (click to toggle)
python-binary-memcached 0.30.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 288 kB
  • sloc: python: 1,702; makefile: 17
file content (162 lines) | stat: -rw-r--r-- 5,320 bytes parent folder | download | duplicates (4)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import six

from bmemcached.client.constants import PICKLE_PROTOCOL, SOCKET_TIMEOUT
from bmemcached.compat import pickle
from bmemcached.protocol import Protocol


class ClientMixin(object):
    """ Client mixin with basic commands.

    :param servers: A list of servers with ip[:port] or unix socket.
    :type servers: list
    :param username: If your server requires SASL authentication, provide the username.
    :type username: six.string_types
    :param password: If your server requires SASL authentication, provide the password.
    :type password: six.string_types
    :param compression: This memcached client uses zlib compression by default,
        but you can change it to any Python module that provides
        `compress` and `decompress` functions, such as `bz2`.
    :type compression: Python module
    :param socket_timeout: The timeout applied to memcached connections.
    :type socket_timeout: float
    :param pickle_protocol: The pickling protocol to use, 0-5. See
        https://docs.python.org/3/library/pickle.html#data-stream-format
        default is 0 (human-readable, original format).
    :type pickle_protocol: int
    :param pickler: Use this to replace the object serialization mechanism.
    :type pickler: function
    :param unpickler: Use this to replace the object deserialization mechanism.
    :type unpickler: function
    :param tls_context: A TLS context in order to connect to TLS enabled
        memcached servers.
    :type tls_context: ssl.SSLContext
    """
    def __init__(self, servers=('127.0.0.1:11211',),
                 username=None,
                 password=None,
                 compression=None,
                 socket_timeout=SOCKET_TIMEOUT,
                 pickle_protocol=PICKLE_PROTOCOL,
                 pickler=pickle.Pickler,
                 unpickler=pickle.Unpickler,
                 tls_context=None):
        self.username = username
        self.password = password
        self.compression = compression
        self.socket_timeout = socket_timeout
        self.pickle_protocol = pickle_protocol
        self.pickler = pickler
        self.unpickler = unpickler
        self.tls_context = tls_context
        self.set_servers(servers)

    @property
    def servers(self):
        for server in self._servers:
            yield server

    def set_servers(self, servers):
        """
        Iter to a list of servers and instantiate Protocol class.

        :param servers: A list of servers
        :type servers: list
        :return: Returns nothing
        :rtype: None
        """
        if isinstance(servers, six.string_types):
            servers = [servers]

        assert servers, "No memcached servers supplied"
        self._servers = [Protocol(
            server=server,
            username=self.username,
            password=self.password,
            compression=self.compression,
            socket_timeout=self.socket_timeout,
            pickle_protocol=self.pickle_protocol,
            pickler=self.pickler,
            unpickler=self.unpickler,
            tls_context=self.tls_context,
        ) for server in servers]

    def flush_all(self, time=0):
        """
        Send a command to server flush|delete all keys.

        :param time: Time to wait until flush in seconds.
        :type time: int
        :return: True in case of success, False in case of failure
        :rtype: bool
        """
        returns = []
        for server in self.servers:
            returns.append(server.flush_all(time))

        return any(returns)

    def stats(self, key=None):
        """
        Return server stats.

        :param key: Optional if you want status from a key.
        :type key: six.string_types
        :return: A dict with server stats
        :rtype: dict
        """
        # TODO: Stats with key is not working.

        returns = {}
        for server in self.servers:
            returns[server.server] = server.stats(key)

        return returns

    def disconnect_all(self):
        """
        Disconnect all servers.

        :return: Nothing
        :rtype: None
        """
        for server in self.servers:
            server.disconnect()

    def get(self, key, default=None, get_cas=False):
        raise NotImplementedError()

    def gets(self, key):
        raise NotImplementedError()

    def get_multi(self, keys, get_cas=False):
        raise NotImplementedError()

    def set(self, key, value, time=0, compress_level=-1):
        raise NotImplementedError()

    def cas(self, key, value, cas, time=0, compress_level=-1):
        raise NotImplementedError()

    def set_multi(self, mappings, time=0, compress_level=-1):
        raise NotImplementedError()

    def add(self, key, value, time=0, compress_level=-1):
        raise NotImplementedError()

    def replace(self, key, value, time=0, compress_level=-1):
        raise NotImplementedError()

    def delete(self, key, cas=0):  # type: (six.string_types, int) -> bool
        raise NotImplementedError()

    def delete_multi(self, keys):
        raise NotImplementedError()

    def incr(self, key, value):
        # TODO: Implement missing parameters
        raise NotImplementedError()

    def decr(self, key, value):
        # TODO: Implement missing parameters
        raise NotImplementedError()