Em 15-05-2012 02:01, Andrew White escreveu:
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.

This would be equivalent to caches_page, while I need the authorization filters to run.

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.

Thanks, I'll try to find some free time to take a look at how caches_action works and will try to issue a pull request if that won't demand me too much time.

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

Please, don't get me wrong. I wasn't trying to say that Rails won't allow me to do what I want to. I was just asking if we could try making such common setup easier to implement in Rails and avoid duplication.

If we accepted "get '/test/index', format: :json, as: :test_index" to just mean that, we'd only need "expire_action :test_index" which reads much better and is less error prone. Also we shouldn't need any filters as the MIME could be extracted from this kind of routing rule.

So your above example could be written as:

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

class TestController<  ApplicationController
  caches_action :index

  def index
    render :json =>  params
  end

  def expire
    expire_action :index
    head :ok
  end
end

I'm asking if it would be possible to support this in Rails 4, for example.

I'm really sorry if I'm bothering you with all those requests. This really isn't my intention and I do value a lot all your feedback.

I'm just trying to see if we could make Rails easier for non multi-format RESTful applications as well.

Thank you very much,

Rodrigo.

--
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