Clearing ActiveRecord cache

2019-03-12 03:40发布

I'm building a command line application using ActiveRecord 3.0 (without rails). How do I clear the query cache that ActiveRecord maintains?

4条回答
smile是对你的礼貌
2楼-- · 2019-03-12 04:12

We use:

ActiveRecord::Base.connection.query_cache.clear
(ActiveRecord::Base.connection.tables - %w[schema_migrations versions]).each do |table|
  table.classify.constantize.reset_column_information rescue nil
end

But I am not certain even this is enough.

查看更多
乱世女痞
3楼-- · 2019-03-12 04:14
神经病院院长
4楼-- · 2019-03-12 04:20

Oftentimes when you see caching of database queries, your db is doing the caching, not ActiveRecord, which means you need to clear the cache and buffers at the db level, not the ActiveRecord level.

For example, to clear Postgres' cache and buffers on Mac, you would do sudo purge, which forces the disk cache to be flushed and emptied.

To clear Postgres' cache and buffers on Linux, you would shut down postgres, drop the caches, and start postgres back up again:

service postgresql stop
sync
echo 3 > /proc/sys/vm/drop_caches
service postgresql start

Further reading:

查看更多
贼婆χ
5楼-- · 2019-03-12 04:28

To a first approximation:

ActiveRecord::Base.connection.query_cache.clear
查看更多
登录 后发表回答