memcached keys can’t have spaces in them!
I am shouting this, in case some other hapless soul goes Googling.
While adding a bunch of items to a memcached using the python-memcached library, I noticed that things did not really seem to be any faster. This was weird of course. Finally I found that some objects just did not seem to be in the cache at all, and that the client library seemed to be getting ‘confused’. The problem? Some of my keys had spaces in them.
Now of course the gut-feel definition of a key is that it should not have a space, but the docs just say that your keys have to be strings. So you assume any kind of string will do. What it should actually say is: “Strings without spaces”. I confirmed this by checking out the memcached protocol, and indeed it is a text based protocol using spaces in the protocol itsself to separate commands and keys sent to the server.
Would be good if the python client library had a check for this and threw an error. Turns out it does have a check, but it does:
if ord(char) < 32 or ord(char) == 127:
while it should be:
if ord(char) < 33 or ord(char) == 127:
What is even stranger is that there is a test for this case in the code, but in the version currently on PyPI (1.44) the test fails.
Will mail the owner with this small patch, and see if it gets into the project. In the meantime, here’s hoping you don’t bang your head and lose a few hours tracking down such an innocous bug like I just did.
Explore posts in the same categories: Python, Uncategorized, WorkTags: bug, fix, memcached, pypi, Python
You can comment below, or link to this permanent URL from your own site.
June 23, 2009 at 5:27 pm
Helpful post–thanks! But I think you mean “strings without spaces,” not “keys without strings.”
June 24, 2009 at 4:07 am
Thanks for the comment, Dan.
The post has been edited to say what I meant.
November 28, 2009 at 1:27 am
Thanks for this patch, it will be fixed in version 1.45 which I expect to release shortly. I changed it slightly to be:
=== modified file ‘memcache.py’
— memcache.py 2009-11-28 01:21:49 +0000
+++ memcache.py 2009-11-28 01:26:33 +0000
@@ -1059,9 +1059,9 @@
raise Client.MemcachedKeyLengthError(“Key length is > %s”
% SERVER_MAX_KEY_LENGTH)
for char in key:
- if ord(char) < 32 or ord(char) == 127:
+ if ord(char) < 33 or ord(char) == 127:
raise Client.MemcachedKeyCharacterError(
- "Control characters not allowed")
+ "Control and space characters not allowed in keys")
def _doctest():
import doctest, memcache
Thanks,
Sean