2011-08-22 72 views
7

我在应用程序中有一些问题将过期缓存。使用自定义缓存路径到期操作缓存

这里是我的控制器:

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 

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

我已经尝试过使用this technique,但没有成功。它看起来像Dalli,这是我的缓存引擎,不支持正则表达式匹配器。

尝试使用此代码时,我得到这个错误:

expire_fragment(/songs/)

ActiveSupport::Cache::DalliStore does not support delete_matched

我试着使用这行代码,用于调试,但它被忽略。

before_filter only: [:songs] 
    expire_fragment(custom_cache_path) 
end 

我使用的是Rails 3.1.0.rc6,Dalli 1.0.5和Ruby 1.9.2。

+1

你使用正则表达式与达利奇与这个宝石:https://github.com/defconomicron/dalli-store-extensions –

+0

我会试试看,谢谢。 – Oleander

回答

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

您可能还需要检查出的解决方案here。用他的方法,你可以用额外的参数使操作失效。