Commit 4e050321 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki Committed by Tatuya Kamada

rewrite memcache_client.py monkey patch based on python_memcached-1.53.

still keeping backward compatibility with older python_memcached.
parent 8b7d3e34
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Code based on python-memcached-1.45 # Code based on python-memcached-1.53
try: try:
from memcache import _Host, Client, _Error from memcache import _Host, Client, _Error
except ImportError: except ImportError:
pass pass
else: else:
_Host._SOCKET_TIMEOUT = 10 # wait more than 3s is safe _Host._SOCKET_TIMEOUT = 10 # wait more than 3s is safe
# always return string
# https://bugs.launchpad.net/python-memcached/+bug/509712
def readline(self):
buf = self.buffer
recv = self.socket.recv
while True:
index = buf.find('\r\n')
if index >= 0:
break
data = recv(4096)
if not data:
self.mark_dead('Connection closed while reading from %s'
% repr(self))
self.buffer = ''
# (patch)
# return None
return ''
buf += data
self.buffer = buf[index+2:]
return buf[:index]
_Host.readline = readline
# Client._get() should raises an Exception instead of returning try:
# None, in case of connection errors or missing values. from memcache import _ConnectionDeadError
class MemcachedConnectionError(Exception): except ImportError:
pass # BBB
Client.MemcachedConnectionError = MemcachedConnectionError class _ConnectionDeadError(Exception):
pass
Client.MemcachedConnectionError = _ConnectionDeadError
import socket import socket
def _get(self, cmd, key): def _get(self, cmd, key):
self.check_key(key) if getattr(self, 'do_check_key', True):
self.check_key(key)
server, key = self._get_server(key) server, key = self._get_server(key)
if not server: if not server:
# (patch) # (patch)
# return None # return None
raise MemcachedConnectionError raise _ConnectionDeadError
self._statlog(cmd)
try: def _unsafe_get():
server.send_cmd("%s %s" % (cmd, key)) self._statlog(cmd)
rkey = flags = rlen = cas_id = None
if cmd == 'gets':
rkey, flags, rlen, cas_id, = self._expect_cas_value(server)
if rkey:
self.cas_ids[rkey] = cas_id
else:
rkey, flags, rlen, = self._expectvalue(server)
if not rkey: try:
server.send_cmd("%s %s" % (cmd, key))
rkey = flags = rlen = cas_id = None
if cmd == 'gets':
try:
rkey, flags, rlen, cas_id, = self._expect_cas_value(server,
raise_exception=True)
except TypeError:
# BBB
rkey, flags, rlen, cas_id, = self._expect_cas_value(server)
if rkey and self.cache_cas:
self.cas_ids[rkey] = cas_id
else:
try:
rkey, flags, rlen, = self._expectvalue(server,
raise_exception=True)
except TypeError:
# BBB
rkey, flags, rlen, = self._expectvalue(server)
if not rkey:
# (patch)
# return None
raise KeyError
try:
value = self._recv_value(server, flags, rlen)
finally:
try:
server.expect("END", raise_exception=True)
except TypeError:
# BBB
server.expect("END")
except (_Error, socket.error), msg:
if isinstance(msg, tuple): msg = msg[1]
server.mark_dead(msg)
# (patch)
# return None
raise _ConnectionDeadError
return value
try:
return _unsafe_get()
except _ConnectionDeadError:
# retry once
try:
if server.connect():
return _unsafe_get()
# (patch) # (patch)
# return None # return None
raise KeyError raise _ConnectionDeadError
value = self._recv_value(server, flags, rlen) except (_ConnectionDeadError, socket.error), msg:
server.expect("END") server.mark_dead(msg)
except (_Error, socket.error), msg:
if isinstance(msg, tuple): msg = msg[1]
server.mark_dead(msg)
# (patch) # (patch)
# return None # return None
raise MemcachedConnectionError raise _ConnectionDeadError
return value
Client._get = _get Client._get = _get
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment