Clearing ActiveRecord cache

前端 未结 4 1946
时光取名叫无心
时光取名叫无心 2021-02-04 00:02

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

相关标签:
4条回答
  • 2021-02-04 00:45

    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.

    0 讨论(0)
  • 2021-02-04 00:48

    To a first approximation:

    ActiveRecord::Base.connection.query_cache.clear
    
    0 讨论(0)
  • Have a look at the method clear_query_cache in http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/QueryCache.html

    0 讨论(0)
  • 2021-02-04 00:57

    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:

    • See and clear Postgres caches/buffers?
    • Does Postgres provide a command to flush buffer cache?
    • https://linux-mm.org/Drop_Caches
    0 讨论(0)
提交回复
热议问题