如何caches_action配置工作,支持多种格式?多种、格式、工作、caches_action

2023-09-11 00:55:44 作者:微光迷失的尘夏

我有一个导轨行动以回应各种格式,包括AJAX请求的要求,例如:

I have a rails action which responds to requests in various formats including AJAX requests, for example:

   def index
    # do stuff
    respond_to do |format|
      format.html do
        # index.html.erb
      end
      format.js do
        render :update do |page|
          page.replace_html 'userlist', :partial => "userlist", :object=>@users
          page.hide('spinner')
          page.show('pageresults')
        end
      end
    end
   end

我已将这次行动使用缓存使用memcached的:

I have set this action to cache using memcached using:

 caches_action :index, :expires_in=>1.hour, :cache_path => Proc.new { |c| "index/#{c.params[:page]}/#{c.request.format}" }

该模式似乎做工精细的缓存HTML结果,但不是JS的结果。 JS的部分始终正常工作时,它不会从缓存中来了。然而,当有一个高速缓存命中,该网页不更新。

This pattern seems to work fine for caching the HTML result but not for the JS result. The JS part always works fine when it is not coming from the cache. However when there is a cache hit, the page does not update.

这是什么原因,什么是修复?

What could cause this and what is the fix?

更新:挖掘到这种更看上去,而不是文/ JavaScript的就像从缓存请求得到MIME类型text / html的'。但是我不知道如何解决这个问题 - 是它的memcached的怪癖? (Rails的2.3.2)

Update: digging into this more it looks like requests from the cache get mime type 'text/html' instead of 'text/javascript'. However I'm not sure how to fix this - is it a quirk of memcached? (Rails 2.3.2)

推荐答案

类似voldy的答案,但使用pcated非德$ P $的方法。

Similar to voldy's answer but using non-deprecated methods.

caches_action :show,
              :cache_path => :post_cache_path.to_proc,
              :expires_in => 1.hour

protected

def post_cache_path
  if request.xhr?
    "#{request.url}.js"
  else
    "#{request.url}.html"
  end
end