gpt4 book ai didi

ruby-on-rails - 使用自定义 cache_path 使操作缓存过期

转载 作者:行者123 更新时间:2023-12-04 08:56:01 25 4
gpt4 key购买 nike

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

这是我的 Controller :

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。

最佳答案

before_filter由于 Action 缓存,块被忽略。
解决方案是改用片段缓存。

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

关于ruby-on-rails - 使用自定义 cache_path 使操作缓存过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7145614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com