gpt4 book ai didi

ruby-on-rails - rails : Skip controller if cache fragment exist (with cache_key)

转载 作者:行者123 更新时间:2023-12-04 03:38:29 26 4
gpt4 key购买 nike

我已经使用缓存很长时间了,最​​近发现我的片段缓存并没有像往常一样阻止我的 Controller 执行代码。我已将问题归结为与似乎是新功能的 cache_key 有关?

这是我以前的解决方案,不再按预期工作。

产品#Show-view:

cache('product-' + @product.id.to_s)  do
# View stuff
end

Product#Show-controller:

unless fragment_exist?('product-19834') # ID obviously dynamically loaded
# Perform slow calculations
end

缓存工作正常。它写入和读取片段,但它仍然执行 Controller (这是我想使用缓存的全部原因)。这归结为片段具有添加的唯一 id,因此创建的片段类似于:

views/product-19834/b05c4ed1bdb428f73b2c73203769b40f

因此,当我检查 fragment_exist 是否存在时,我没有检查正确的字符串(因为我正在检查“views/product-19834”)。我也试过用:

fragment_exist?("product-#{@product.id}/#{@product.cache_key}")

但它检查的 cache_key 与实际创建的不同。

我宁愿使用这种解决方案,也不愿使用 Controller 缓存或联锁等 gem 。

我的问题是:- 考虑到这个缓存键,我如何在 Controller 中检查特定 View 的片段是否存在?

最佳答案

正如 Kelseydh 在链接中指出的那样,解决方案是在缓存请求中使用 skip_digest => true:

查看

cache ("product" + @product.id, :skip_digest => true) 

Controller

fragment_exist?("product-#{@product.id}")

关于ruby-on-rails - rails : Skip controller if cache fragment exist (with cache_key),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32138119/

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