Bug 957385: memcached requires keys be bytestrings; r=catlee
authorDustin J. Mitchell <dustin@mozilla.com>
Wed, 22 Jan 2014 08:36:59 -0500
changeset 232 03dce877d18892baeecdf200da1785a12488412e
parent 231 12a41f69831787ae1f8364186a74c6a8d7b2123c
child 233 909150ceda8f89d1d27e9659cb02816a3719c8f2
push id186
push userdmitchell@mozilla.com
push dateWed, 22 Jan 2014 13:44:24 +0000
reviewerscatlee
bugs957385
Bug 957385: memcached requires keys be bytestrings; r=catlee
buildapi/lib/cacher.py
--- a/buildapi/lib/cacher.py
+++ b/buildapi/lib/cacher.py
@@ -63,32 +63,37 @@ try:
         def has_key(self, key):
             return self.r.exists(key)
 
 except ImportError:
     pass
 
 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:
                 return json.loads(retval)
 
         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