【发布时间】:2014-12-21 12:04:10
【问题描述】:
我的 Rails 应用程序中的查询运行时间不长(约 180 毫秒),但确实返回了很多结果。结果在 API 中使用,结果到 JSON 的转换很昂贵(约 3 秒)。为了加快速度,我像这样缓存它:
key = 'new_prices'
query = Prices.new_this_month.where('...')
json = cache(key, expires_in: 60.minutes) do
render_to_string json: query, each_serializer: PriceSerializer
end
render json: json
在后台,可以将新价格添加到数据库中。如何生成缓存键,如果query的结果发生变化,缓存就会失效?
我正在使用 Rails 3.2,通过 dalli gem 使用 memcached。
【问题讨论】:
标签: ruby-on-rails activerecord memcached dalli