Search code examples
ruby-on-railsrubycachingmemcached

Set raw value with Rails.cache.write


I'm trying to save entire response.body to memcached. I'm doing it like so:

Rails.cache.write(request.headers['HTTP_X_MEMCACHED_KEY'], response.body)

The problem is that it prepends some garbage to the value:

o: ActiveSupport::Cache::Entry  :@compressedF:@expires_in0:@created_atf1355928410.584484:@value"GsI";s<!DOCTYPE html>...

I tried to pass :raw => true to Rails.cache.write, but it returns false and doesn't put the value to memcached. I think it fails because response.body is not properly escaped.

I also tried this way:

Rails.cache.write(request.headers['HTTP_X_MEMCACHED_KEY'], Marshal.dump(response.body), :raw => true)

It works, but there's still some garbage in the value:

I"fD<!DOCTYPE html>...

How to put a clean value into memcached?


Solution

  • It seems memcached was unable to cache data because of unescaped unicode characters in response.body.

    Now I'm passing response.body.bytes.to_a.map(&:chr).join as the value, it works fine, but I'm still wondering if it is the best solution.