如何从Ruby禁用ActiveRecord的QueryCache

时间:2010-12-15 08:15:09

标签: ruby-on-rails ruby activerecord

我想从ruby中禁用ActiveRecord的查询缓存,但它不起作用,代码如下:

def test_transaction
  ActiveRecord::Base.connection.execute("create table if not exists t(name varchar(100))")
  ActiveRecord::Base.connection.uncached do
  begin
    ActiveRecord::Base.transaction do
      ActiveRecord::Base.connection.execute("insert into t values('one')")
      ActiveRecord::Base.connection.execute("insert into t values('two')")
      ActiveRecord::Base.connection.execute("insert into t values('three')")
      ActiveRecord::Base.connection.execute("insert into t values('four')")
      result = ActiveRecord::Base.connection.select_all("select count(*) as total from t")[0]['total'].to_i
      assert_equal(0, result)
    end
  ensure
    ActiveRecord::Base.connection.execute("drop table if exists t")
  end
end
end

事实上,当代码在select之前运行时,我通过控制台登录mysql并执行相同的select查询,结果总数为零,所以没关系,但是在代码执行select之后,结果是4,也就是说必须从查询缓存中获取select,但这是我不想要的,所以任何人都可以为我提供任何帮助吗?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

直接在mysql控制台上做实验后发现它与ActiveRecord无关,这是由于mysql事务的机制。