gpt4 book ai didi

ruby-on-rails - Rails 4 在 Rails.cache.fetch 中跳过缓存

转载 作者:行者123 更新时间:2023-12-04 15:44:05 25 4
gpt4 key购买 nike

我的代码:

def my_method(path)
Rails.cache.fetch(path, expires_in: 10.minutes, race_condition_ttl: 10) do
# Calls Net::HTTP.get_response URI.parse path
response = My::api.get path

if response.status == 200
JSON.parse response.body
else
nil # Here I need to prevent cache
end
end
end

返回时我不会缓存 nil ,但它确实..
在这种情况下如何防止缓存?

最佳答案

一种不太优雅的方法是提高错误。

def my_method(path)
Rails.cache.fetch(path, expires_in: 10.minutes, race_condition_ttl: 10) do
# Calls Net::HTTP.get_response URI.parse path
response = My::api.get path

raise MyCachePreventingError unless response.status == 200

JSON.parse response.body
end
rescue MyCachePreventingError
nil
end

如果有人有更好的方法我想知道

关于ruby-on-rails - Rails 4 在 Rails.cache.fetch 中跳过缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25498596/

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