跳至內容 跳至搜尋
方法
C
D
E
N
Q
U

屬性

[讀寫] query_cache

類別公開方法

dirties_query_cache(base, *method_names)

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 20
        def dirties_query_cache(base, *method_names)
          method_names.each do |method_name|
            base.class_eval <<-end_code, __FILE__, __LINE__ + 1
              def #{method_name}(...)
                if pool.dirties_query_cache
                  ActiveRecord::Base.clear_query_caches_for_current_thread
                end
                super
              end
            end_code
          end
        end

new(*)

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 196
def initialize(*)
  super
  @query_cache = nil
end

實例公開方法

cache(&block)

在程式區塊內啟用查詢快取。

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 206
def cache(&block)
  pool.enable_query_cache(&block)
end

clear_query_cache()

清除查詢快取。

您可能希望明確呼叫此方法的一個原因是在要求資料庫隨機化結果的查詢之間。否則,快取會看到相同的 SQL 查詢,並在每次都重複返回相同的結果,從而默默地破壞您期望的隨機性。

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 232
def clear_query_cache
  pool.clear_query_cache
end

disable_query_cache!()

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 222
def disable_query_cache!
  pool.disable_query_cache!
end

enable_query_cache!()

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 210
def enable_query_cache!
  pool.enable_query_cache!
end

query_cache_enabled()

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 201
def query_cache_enabled
  @query_cache&.enabled?
end

uncached(dirties: true, &block)

在程式區塊內停用查詢快取。

設定 dirties: false 可以防止所有連線上的查詢快取因寫入操作而被清除。(預設情況下,寫入操作會將所有連線的查詢快取標記為已變更,以防它們是快取現已過期的副本。)

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 218
def uncached(dirties: true, &block)
  pool.disable_query_cache(dirties: dirties, &block)
end