在Rails中暂时禁用SQL缓存?

时间:2022-02-15 03:54:11

I'm currently creating a Rails app with some cronjobs etc, but I have some problems because the sql is cached by Rails.

我目前正在创建一个带有一些cronjobs等的Rails应用程序,但是我有一些问题因为sql是由Rails缓存的。

So anyone know how to disable the SQL Cache in Rails? Not globally, but for this code. Really don't want to create one method for every model, so is there anyway to just disable it temporary?

所以任何人都知道如何在Rails中禁用SQL Cache?不是全局的,而是针对此代码。真的不想为每个模型创建一个方法,所以无论如何只是暂时禁用它?

Terw

Terw

3 个解决方案

#1


20  

Solved with model.connection.clear_query_cache

解决了model.connection.clear_query_cache

#2


10  

Another approach is using ActiveRecord::QueryCache::ClassMethods#uncached

另一种方法是使用ActiveRecord :: QueryCache :: ClassMethods #accached

Model.uncached do
  # your code
end

#3


5  

What specifically worked for me was:

具体对我有用的是:

ActiveRecord::Base.connection.clear_query_cache

With credit for good explanations going to this blog post.

这篇博客文章有很好的解释。

#1


20  

Solved with model.connection.clear_query_cache

解决了model.connection.clear_query_cache

#2


10  

Another approach is using ActiveRecord::QueryCache::ClassMethods#uncached

另一种方法是使用ActiveRecord :: QueryCache :: ClassMethods #accached

Model.uncached do
  # your code
end

#3


5  

What specifically worked for me was:

具体对我有用的是:

ActiveRecord::Base.connection.clear_query_cache

With credit for good explanations going to this blog post.

这篇博客文章有很好的解释。