Rails 5 缓存在本地或生产环境中不起作用
Posted
技术标签:
【中文标题】Rails 5 缓存在本地或生产环境中不起作用【英文标题】:Rails 5 caching isn't working locally or in production 【发布时间】:2020-07-26 11:58:01 【问题描述】:我正在尝试从这个外部数据源获取数据并将其显示在我的本地应用程序和生产环境中,并且由于每次请求新鲜数据可能会占用大量资源,因此我想将其缓存相对数量时间可能是 15 分钟、1 小时等。我编写了这段代码,但它根本没有显示任何类型的缓存。
covid_controller.rb
require "net/http"
class Covid19::CovidController < ApplicationController
def index
@covid_news_posts = CovidNewsPost.published.limit(10).order("created_at DESC").includes([:user])
cache_key_with_version = CovidNewsPost.last
@cache = Rails.cache.fetch("#cache_key_with_version", expires_in: 15.minutes) do
covid_api_url = "https://bing.com/covid/data"
resp = Net::HTTP.get_response(URI.parse(covid_api_url))
covidapi = JSON.parse(resp.body)
end
end
end
生产.rb
## CACHING RELATED THINGS
config.action_controller.perform_caching = true
# Use a different cache store in production.
config.cache_store = :memory_store, size: 64.megabytes
开发.rb
# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.cache_store = :memory_store
config.public_file_server.headers =
'Cache-Control' => "public, max-age=#2.days.seconds.to_i"
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
这里不知道该怎么做,只想加载请求服务器端一次,然后将其缓存到需要的时间。
这是来自生产的请求,没有显示任何缓存
I, [2020-04-13T20:46:40.057029 #2088] INFO -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] Started GET "/covid19" for 71.113.156.118 at 2020-04-13 20:46:40 +0000
I, [2020-04-13T20:46:40.060499 #2088] INFO -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] Processing by Covid19::CovidController#index as html
D, [2020-04-13T20:46:40.063881 #2088] DEBUG -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
D, [2020-04-13T20:46:40.065930 #2088] DEBUG -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] CovidNewsPost Load (0.6ms) SELECT "covid_news_posts".* FROM "covid_news_posts" ORDER BY "covid_news_posts"."id" DESC LIMIT $1 [["LIMIT", 1]]
I, [2020-04-13T20:46:40.458451 #2088] INFO -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] Rendering layouts/application.html.erb
I, [2020-04-13T20:46:40.458928 #2088] INFO -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] Rendering covid19/covid/index.html.erb within layouts/application
I, [2020-04-13T20:46:40.460280 #2088] INFO -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] Rendered covid19/_covid19_menu.html.erb (0.3ms)
D, [2020-04-13T20:46:40.462798 #2088] DEBUG -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] CovidNewsPost Load (1.7ms) SELECT "covid_news_posts".* FROM "covid_news_posts" WHERE "covid_news_posts"."published" = $1 ORDER BY created_at DESC LIMIT $2 [["published", true], ["LIMIT", 10]]
D, [2020-04-13T20:46:40.465690 #2088] DEBUG -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" IN ($1, $2, $3, $4, $5, $6) [["id", 251], ["id", 3], ["id", 860], ["id", 208], ["id", 1985], ["id", 2794]]
I, [2020-04-13T20:46:40.476577 #2088] INFO -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] Rendered static/global/_footer.html.erb (1.6ms)
I, [2020-04-13T20:46:40.476792 #2088] INFO -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] Rendered covid19/covid/index.html.erb within layouts/application (17.8ms)
I, [2020-04-13T20:46:40.477422 #2088] INFO -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] Rendered application/_favicon.html.erb (0.4ms)
I, [2020-04-13T20:46:40.478655 #2088] INFO -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] Rendered layouts/application.html.erb (20.1ms)
I, [2020-04-13T20:46:40.479248 #2088] INFO -- : [aee0a42a-0d29-432d-af0e-0f21bb5253d9] Completed 200 OK in 419ms (Views: 18.4ms | ActiveRecord: 3.9ms)
【问题讨论】:
【参考方案1】:cache_key_with_version
分配给CovidNewsPost.last
,这可能是一个 ActiveRecord 实例。然后将其转换为字符串并用作缓存键。此字符串的任何变化都将导致获取和填充新的缓存条目,并且不一定保证记录的字符串表示在任何特定方式下都是一致的。
您可能希望使用更具体的字符串,例如"#cache_key_with_version.id-#cache_key_with_version.version"
,以确保只有您打算成为缓存键一部分的字段实际上是缓存键的一部分。
除此之外,您可以使用 Rails 控制台验证缓存是否配置正确:
2.times.map Rails.cache.fetch("test", expires_in: 1) rand .uniq.length == 1
如果缓存配置并正常工作,您将看到true
。如果没有,你会得到false
。
【讨论】:
以上是关于Rails 5 缓存在本地或生产环境中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Mailform 在本地工作,但不在生产环境中(Heroku、Rails)