跳到內容 跳到搜尋
命名空間
方法
C
D
E
N
U

屬性

[R] query_cache
[R] query_cache_enabled

類別公開方法

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}(...)
                ActiveRecord::Base.clear_query_caches_for_current_thread
                super
              end
            end_code
          end
        end

new(*)

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 55
def initialize(*)
  super
  @query_cache         = {}
  @query_cache_enabled = false
  @query_cache_max_size = nil
end

執行個體公開方法

cache()

在區塊內啟用查詢快取。

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 63
def cache
  old, @query_cache_enabled = @query_cache_enabled, true
  yield
ensure
  @query_cache_enabled = old
  clear_query_cache unless @query_cache_enabled
end

clear_query_cache()

清除查詢快取。

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

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 94
def clear_query_cache
  @lock.synchronize do
    @query_cache.clear
  end
end

disable_query_cache!()

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 75
def disable_query_cache!
  @query_cache_enabled = false
  clear_query_cache
end

enable_query_cache!()

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 71
def enable_query_cache!
  @query_cache_enabled = true
end

uncached()

在區塊內停用查詢快取。

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 81
def uncached
  old, @query_cache_enabled = @query_cache_enabled, false
  yield
ensure
  @query_cache_enabled = old
end