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.

About these ads
Explore posts in the same categories: Python, Uncategorized, Work

Tags: , , , ,

You can comment below, or link to this permanent URL from your own site.

3 Comments on “memcached keys can’t have spaces in them!”

  1. dan Says:

    Helpful post–thanks! But I think you mean “strings without spaces,” not “keys without strings.”

  2. pos.thum.us Says:

    Thanks for the comment, Dan.
    The post has been edited to say what I meant.


  3. 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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: