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:
# BBB
class _ConnectionDeadError(Exception):
pass pass
Client.MemcachedConnectionError = MemcachedConnectionError Client.MemcachedConnectionError = _ConnectionDeadError
import socket import socket
def _get(self, cmd, key): def _get(self, cmd, key):
if getattr(self, 'do_check_key', True):
self.check_key(key) 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
def _unsafe_get():
self._statlog(cmd) self._statlog(cmd)
try: try:
server.send_cmd("%s %s" % (cmd, key)) server.send_cmd("%s %s" % (cmd, key))
rkey = flags = rlen = cas_id = None rkey = flags = rlen = cas_id = None
if cmd == 'gets': 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) rkey, flags, rlen, cas_id, = self._expect_cas_value(server)
if rkey: if rkey and self.cache_cas:
self.cas_ids[rkey] = cas_id self.cas_ids[rkey] = cas_id
else: else:
try:
rkey, flags, rlen, = self._expectvalue(server,
raise_exception=True)
except TypeError:
# BBB
rkey, flags, rlen, = self._expectvalue(server) rkey, flags, rlen, = self._expectvalue(server)
if not rkey: if not rkey:
# (patch) # (patch)
# return None # return None
raise KeyError raise KeyError
try:
value = self._recv_value(server, flags, rlen) value = self._recv_value(server, flags, rlen)
finally:
try:
server.expect("END", raise_exception=True)
except TypeError:
# BBB
server.expect("END") server.expect("END")
except (_Error, socket.error), msg: except (_Error, socket.error), msg:
if isinstance(msg, tuple): msg = msg[1] if isinstance(msg, tuple): msg = msg[1]
server.mark_dead(msg) server.mark_dead(msg)
# (patch) # (patch)
# return None # return None
raise MemcachedConnectionError raise _ConnectionDeadError
return value return value
try:
return _unsafe_get()
except _ConnectionDeadError:
# retry once
try:
if server.connect():
return _unsafe_get()
# (patch)
# return None
raise _ConnectionDeadError
except (_ConnectionDeadError, socket.error), msg:
server.mark_dead(msg)
# (patch)
# return None
raise _ConnectionDeadError
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