Skip to content

Add support for raw data flag #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bmemcached/client/replicating.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ class ReplicatingClient(object):
:type unpickler: function
:param socket_timeout: The timeout applied to memcached connections.
:type socket_timeout: float
:param raw_data: Whether this client should read and write only binary strings, rather than storing type information in the 'opaque' field. This is useful when interacting with memcached keys which already have the 'opaque value' set to something, to avoid misinterpreting data.
:type raw_data: bool
"""
def __init__(self, servers=('127.0.0.1:11211',), username=None,
password=None, compression=None,
socket_timeout=SOCKET_TIMEOUT,
pickle_protocol=0,
pickler=pickle.Pickler,
unpickler=pickle.Unpickler):
unpickler=pickle.Unpickler,
raw_data=False):
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.raw_data = raw_data
self.set_servers(servers)

@property
Expand Down Expand Up @@ -69,6 +73,7 @@ def set_servers(self, servers):
pickle_protocol=self.pickle_protocol,
pickler=self.pickler,
unpickler=self.unpickler,
raw_data=self.raw_data,
) for server in servers]

def _set_retry_delay(self, value):
Expand Down
13 changes: 12 additions & 1 deletion bmemcached/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Protocol(threading.local):
COMPRESSION_THRESHOLD = 128

def __init__(self, server, username=None, password=None, compression=None, socket_timeout=None,
pickle_protocol=None, pickler=None, unpickler=None):
pickle_protocol=None, pickler=None, unpickler=None, raw_data=False):
super(Protocol, self).__init__()
self.server = server
self._username = username
Expand All @@ -122,6 +122,8 @@ def __init__(self, server, username=None, password=None, compression=None, socke
self.host = self.port = None
self.set_retry_delay(0)

self.raw_data = raw_data

@property
def server_uses_unix_socket(self):
return self.host is None
Expand Down Expand Up @@ -332,6 +334,12 @@ def serialize(self, value, compress_level=-1):
:rtype: str
"""
flags = 0

if self.raw_data:
if compress_level > 0:
raise MemcachedException("Compression is not possible when in 'raw data' mode")
return flags, value

if isinstance(value, binary_type):
flags |= self.FLAGS['binary']
elif isinstance(value, text_type):
Expand Down Expand Up @@ -376,6 +384,9 @@ def deserialize(self, value, flags):
"""
FLAGS = self.FLAGS

if self.raw_data:
return value

if flags & FLAGS['compressed']: # pragma: no branch
value = self.compression.decompress(value)

Expand Down