Rails缓存


price_from = 10
price_to = 20
option = "coupon_final_price >= 10 and coupon_final_price < 20"
order_by = "weight"
asc_or_desc = "desc"
cache_key = "product-coupon-final-price-from-#{price_from}-to-#{price_to}-order-by-#{order_by}-#{asc_or_desc}"


cache_key = "product-coupon-final-price-from-#{price_from}-to-#{price_to}-order-by-#{order_by}-#{asc_or_desc}-#{page_no}-#{per_num}"



results = Rails.cache.fetch("#{cache_key}/list", expires_in: 10.minutes) do
  Material.where("#{option}").order("#{order_by} #{asc_or_desc}").page(page_no).per(per_num)
end



这样写就可以了,10 分钟有效期

Rails.cache.fetch do end 中的结果,在 expires_in:  时间内会失效一次

阅读量: 334
发布于:
修改于: