Permalink
Browse files

Bug 957385: memcached requires keys be bytestrings; r=catlee

  • Loading branch information...
1 parent 8cee8e4 commit 5d9bfe3d30e94b9ebc827492ac1f9138ffe77be3 @djmitche djmitche committed Jan 22, 2014
Showing with 9 additions and 4 deletions.
  1. +9 −4 buildapi/lib/cacher.py
View
@@ -68,12 +68,17 @@ def has_key(self, key):
try:
import memcache
+ def utf8(s):
+ if isinstance(s, unicode):
+ return s.encode('utf-8')
+ return s
+
class MemcacheCache(BaseCache):
def __init__(self, hosts=['localhost:11211']):
self.m = memcache.Client(hosts)
def _get(self, key):
- retval = self.m.get(key)
+ retval = self.m.get(utf8(key))
if retval is None:
raise KeyError
else:
@@ -82,13 +87,13 @@ def _get(self, key):
def _put(self, key, val, expire=0):
val = json.dumps(val)
if expire == 0:
- self.m.set(key, val)
+ self.m.set(utf8(key), val)
else:
expire = int(expire - time.time())
- self.m.set(key, val, expire)
+ self.m.set(utf8(key), val, expire)
def has_key(self, key):
- return self.m.get(key) is not None
+ return self.m.get(utf8(key)) is not None
except ImportError:
pass

0 comments on commit 5d9bfe3

Please sign in to comment.