Dave Perrett

Nginx Adds Strange Characters Using Rails/memcached_pass

linux, memcached, nginx, programming, quickie, rails, ruby, web

After upgrading a few gems recently, I found that the content returned from the nginx memcached_pass directive always had a few strange characters prepended.

After some investigation it seems that this is caused by rails ‘marshalling’ the data. If you’re using a memcached set command such as

Cache.set(key, content, time_to_live)

You need to add an extra argument to force rails to cache the raw data instead of marshalling it :

Cache.set(key, content, time_to_live, true)

Restart memcached to make sure the old data is cleared, and the problem should go away.