Question

I'm having a bit of a problem with memcaching the pages generate with my rack app.

I'm storing the page generated by my rack app in memcache with the following bit of (ruby) code:

require 'dalli'
memcached = Dalli::Client.new("localhost:11211")
memcached.set(req.path_info, response[2][0])

(where response[2][0] is the generated html code)

In my nginx server config I have the following:

location @rack {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://127.0.0.1:9292;
}
location @memcached {
  set $memcached_key $request_uri;
  memcached_pass localhost:11211;
  default_type text/html;
  error_page 404 = @rack;
}
location / {try_files @memcached;}

This kinda works, but not completely: the content passed to my browser now starts with:

I"¯[<!DOCTYPE html><html ...

My question is: what's the extra bit in front of the html code, and how do I prevent it from showing in the browser result?

Was it helpful?

Solution

Dalli uses Marshal.dump to serialize the value you set (so that you can cache arbitrary ruby objects), so what nginx gets isn't just the string but data in ruby's marshal format. The extra bytes you see contain the marshal header (version of the format etc.) and bytes that say that the bytes that follow are a string.

You can tell dalli to store the raw value of the object instead:

memcached.set(req.path_info, response[2][0], nil, :raw => true)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top