【发布时间】:2020-10-07 20:19:16
【问题描述】:
我将graphql 用于我的rails api。并使用redis 进行缓存存储。
在 graphql 查询中,我使用缓存来优化性能:
graphql/types/query_type.rb
field :labels, [Types::LabelType], null: false do
description 'Get all food labels'
end
def labels
Rails.cache.fetch("labels", expires_in: 24.hours) do
Label.all
end
end
在数据库中播种了 10 个标签,当我在 localhost:3000/graphiql 中测试查询时,它正确显示了 10 个结果。但是,如果我在数据库中手动删除一行,它会返回 9 条记录,而不是缓存 10 条结果。
这是我的环境配置:
config/environments/development.rb
if Rails.root.join('tmp', 'caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.cache_store = :redis_cache_store, { url: ENV.fetch("REDIS_URL_CACHING", "redis://localhost:6379/0") }
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.to_i}"
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
我跑了rails dev:cache,在tmp 目录中有caching-dev.txt。
我在这里错过了什么?
【问题讨论】:
-
你的意思是
rails dev:cache? -
是的,抱歉打错了。
-
我认为我对这个关于Model Fragment Caching 的问题的回答可能与此相关。 (不确定问题本身是否足够接近重复,并且提问者说答案对他们没有帮助,但我觉得这可能是我认为是他们的问题)
-
@SimpleLime 您好,感谢您的关注。我想出了一个解决方案,但我猜不出背后的原因。有什么建议吗?
标签: ruby-on-rails ruby caching redis graphql