Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

Commit

Permalink
Bug 957385: memcached requires keys be bytestrings; r=catlee
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Jan 22, 2014
1 parent 8cee8e4 commit 5d9bfe3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions buildapi/lib/cacher.py
Expand Up @@ -68,12 +68,17 @@ def has_key(self, key):


try: try:
import memcache import memcache
def utf8(s):
if isinstance(s, unicode):
return s.encode('utf-8')
return s

class MemcacheCache(BaseCache): class MemcacheCache(BaseCache):
def __init__(self, hosts=['localhost:11211']): def __init__(self, hosts=['localhost:11211']):
self.m = memcache.Client(hosts) self.m = memcache.Client(hosts)


def _get(self, key): def _get(self, key):
retval = self.m.get(key) retval = self.m.get(utf8(key))
if retval is None: if retval is None:
raise KeyError raise KeyError
else: else:
Expand All @@ -82,13 +87,13 @@ def _get(self, key):
def _put(self, key, val, expire=0): def _put(self, key, val, expire=0):
val = json.dumps(val) val = json.dumps(val)
if expire == 0: if expire == 0:
self.m.set(key, val) self.m.set(utf8(key), val)
else: else:
expire = int(expire - time.time()) expire = int(expire - time.time())
self.m.set(key, val, expire) self.m.set(utf8(key), val, expire)


def has_key(self, key): def has_key(self, key):
return self.m.get(key) is not None return self.m.get(utf8(key)) is not None


except ImportError: except ImportError:
pass pass

0 comments on commit 5d9bfe3

Please sign in to comment.