Set the initial value using Rails.cache.write

I am trying to save all response.body to memcached. I do it like this:

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

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

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

I tried passing :raw => true to Rails.cache.write , but it returns false and does not put the value in memcached. I think this fails because response.body failed to escape.

I also tried like this:

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

It works, but there is still garbage in it:

 I"fD<!DOCTYPE html>... 

How to put net worth in memcached?

+4
source share
1 answer

Memcached seems to be unable to cache data due to unescaped Unicode characters in response.body .

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

+1
source

All Articles