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.