保存记录后,ActiveRecord查询缓存被清除

时间:2013-07-29 10:45:28

标签: ruby-on-rails activerecord

我发现保存记录后清除了查询缓存。有谁知道这是什么问题?请参阅以下代码。

ActiveRecord::Base.cache do 
  policy_file = FormUnit.find_by_number("F.XXX")

  # @query_cache has a value that is a query of policy_file
  pp ActiveRecord::Base.connection.instance_variable_get("@query_cache")

  # fetched it from cache
  FormUnit.find_by_number("F.XXX")

  policy_file_branch = policy_file.forms.build(edition: "10/10", effective_date: "2013/1/1")

  # still can fetch it from cache query.
  pp ActiveRecord::Base.connection.instance_variable_get("@query_cache")
  FormUnit.find_by_number("F.XXX")


  policy_file_branch.save

  # now, @query_cache was empty and had to fetch it in sql query.
  pp ActiveRecord::Base.connection.instance_variable_get("@query_cache")
  FormUnit.find_by_number("F.XXX")

end

0 个答案:

没有答案