rob*_*may 9 ruby caching ruby-on-rails
使用如下的Rails.cache.fetch甚至在我的开发环境中缓存(关闭缓存):
@boat_features = Rails.cache.fetch("boat_features", expires_in: 10.minutes) do
BoatFeature.all
end
Run Code Online (Sandbox Code Playgroud)
有没有人遇到过这个?
这很正常.这种缓存在开发过程中没有被关闭.在之前的应用中,这是一个问题,我们使用内存存储,然后添加了一个Rails.cache.clear在每个请求后执行的中间件.
就像是
config.middleware.use ClearCache
Run Code Online (Sandbox Code Playgroud)
在development.rb
然后你的ClearCache中间件看起来应该是这样的
class ClearCache
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
ensure
Rails.cache.clear
end
end
Run Code Online (Sandbox Code Playgroud)
在Rails 3.2中也有 ActiveSupport::Cache::NullStore
我有同样的问题.我经常工作,然后想出了这个简单的解决方案.在开发配置文件中config/environments/development.rb添加这些设置
config.perform_caching = false
config.cache_store = :null_store