0
我可能在这里丢失了一些非常简单的东西,但不明白是什么。在rails中查询缓存仍然对数据库运行调用
我试图缓存一个简单的活动记录查询,但每次触摸缓存时,都会再次对数据库运行查询。
控制器代码:
products = Rails.cache.read("search_results")
if products
render :text => products[0].id
else
products = Product.where('name LIKE ?", 'product_name?')
Rails.cache.write("search_results", products)
end
我可以看到,在我的第二个电话,我得到的,如果块,而不是其他人,而是任何我想要去触摸产品(如渲染它)时我也查看对数据库的有效记录调用。
我错过了什么?
不是答案,但考虑使用['fetch'](http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html#method-i-fetch)而不是自己读写。 – x1a4 2012-07-30 02:30:52