On 15 May 2012, at 04:02, Rodrigo Rosenfeld Rosas wrote:

> This is different and less useful for my application. That is because I'd 
> like to fill the cache only in the first access no matter which client did 
> request it first. Until that action is expired, only the first client should 
> notice the delay in the first access. All other clients should benefit from 
> this first access as well.

Use Cache-Control: public to share cached resources between clients - obviously 
things like cookies and sessions can't be shared.

> Would you mind explain why it is hard to store the headers alongside with the 
> body in the caches_page mechanism?

The caches_page stores static files that Apache/nginx serve up - Rails doesn't 
even get hit if it's set up properly. If you meant caches_action then that's 
just the way things have been historically as it's just a wrapper around 
fragment caching which obviously doesn't need headers saving. Feel free to 
investigate the feasibility of adding header support to action caching but be 
aware that controller filters will still run for a cached action (action 
caching is an around filter itself) so that things like authentication still 
work so any changes need to make sure that you don't serve cached private data.

>> You can do that now using an empty option in your regexp:
>> 
>> get '/products' =>  'products#index', :format =>  /|json/
>> 
>> This will match /products and /products.json
> 
> Yeah, but then if I accessed /products in a cached action the returned 
> content-type would be text/html, right?

You could exploit the fact that action caching still runs controller filters to 
set the content_type:

TestApp::Application.routes.draw do
  get 'test/index', :as => :index, :format => /|json/
  get 'test/expire', :as => :expire
end

class TestController < ApplicationController
  before_filter :set_content_type, :only => :index
  caches_action :index

  def index
    render :json => params
  end

  def expire
    expire_action :action => :index
    expire_action :action => :index, :format => :json
    head :ok
  end

  def set_content_type
    self.content_type = Mime::JSON
  end
end

This serves cached /test/index and /test/index.json as application/json and 
/test/expire as text/html


Andrew White

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to