squidfunk
squidfunk

Reputation: 431

Exclusively server-side caching of a Rails.app with Rack::Cache

I have the following problem: I want to cache the result of an action in Redis. For this reason, I use https://github.com/jodosha/redis-rack-cache. The fact that an action should be cached by Rack::Cache is determined by setting the appropriate HTTP header information in Rails, e.g.:

response.headers['Cache-Control'] = 'max-age=3600, public, must-revalidate'

Now, Rack::Cache will correctly cache the response in Redis. However, this header does also tell the browser to cache the response, which I don't want! The request should be cached exclusively on the server-side.

As a workaround, I am replacing the header in nginx, which I use as a reverse proxy, but there must be a more elegant way. Does anybody know how to do it?

Best regards, Martin

Upvotes: 1

Views: 297

Answers (1)

rjk
rjk

Reputation: 1472

One option would be to write your own middleware that sits above Rack::Cache and then removes these Cache-Control headers from the response.

Something as simple as:

  def call(env)
    status, headers, body = @app.call(env)
    headers.delete("Cache-Control")
    [status, headers, body]
  end

would work as a middleware.

Upvotes: 1

Related Questions