清除ActiveRecord缓存
我正在使用ActiveRecord 3.0(没有rails)构建命令行应用程序。 如何清除ActiveRecord维护的查询缓存?
第一个近似值:
ActiveRecord::Base.connection.query_cache.clear
在http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/QueryCache.html中查看clear_query_cache
方法。
通常,当您看到数据库查询的缓存时,您的数据库正在执行缓存,而不是ActiveRecord,这意味着您需要在数据库级别而不是ActiveRecord级别清除缓存和缓冲区。
例如,要清除Mac上的Postgres缓存和缓冲区,您可以执行sudo purge
,这会强制刷新和清空磁盘缓存。
要在Linux上清除Postgres的缓存和缓冲区,您可以关闭postgres,删除缓存,然后重新启动postgres:
service postgresql stop sync echo 3 > /proc/sys/vm/drop_caches service postgresql start
进一步阅读:
- 查看并清除Postgres缓存/缓冲区?
- Postgres是否提供刷新缓冲区缓存的命令?
- https://linux-mm.org/Drop_Caches
我们用:
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
但我不确定这还不够。