使用自定义cache_path使操作缓存过期

Lin*_*der 8 ruby-on-rails ruby-on-rails-3 action-caching

我的应用程序中的操作缓存到期时遇到了一些问题.

这是我的控制器:

class ToplistsController < ApplicationController
  caches_action :songs, cache_path: :custom_cache_path.to_proc

  def custom_cache_path
    "#{params[:when]}-#{params[:what]}-#{params[:controller]}-#{params[:action]}"
  end

  def songs
    # ...
  end  
end
Run Code Online (Sandbox Code Playgroud)

我不知何故需要能够重置自定义缓存路径,但我无法弄清楚如何.

我已经尝试过使用这种技术,但没有成功.它看起来像我的缓存引擎Dalli,不支持regexp匹配器.

我在尝试使用此代码时遇到此错误:

expire_fragment(/songs/)

ActiveSupport::Cache::DalliStore does not support delete_matched

我试图使用这行代码进行调试,但它被忽略了.

before_filter only: [:songs] 
  expire_fragment(custom_cache_path)
end
Run Code Online (Sandbox Code Playgroud)

我正在使用Rails 3.1.0.rc6,Dalli 1.0.5和Ruby 1.9.2.

Lin*_*der 0

before_filter由于操作缓存,该块被忽略。
解决方案是使用片段缓存。

# Controller
class ToplistsController < ApplicationController
  helper_method :custom_cache_path

  before_filter only: [:songs]
    if params[:reset_cache]
      expire_fragment(custom_cache_path)
    end
  end

  def custom_cache_path
    "#{params[:when]}-#{params[:what]}-#{params[:controller]}-#{params[:action]}"
  end

  def songs
    # ...
  end  
end

# View

<%= cache custom_cache_path do %>
  Content that should be cached
<% end %>
Run Code Online (Sandbox Code Playgroud)