跳至内容 跳至搜索
方法
C
D
E
N
Q
U

Attributes

[W] query_cache

类公共方法

dirties_query_cache(base, *method_names)

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 18
        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 210
def initialize(*)
  super
  @query_cache = nil
end

实例公共方法

cache(&block)

在此块内启用查询缓存。

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 233
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 259
def clear_query_cache
  pool.clear_query_cache
end

disable_query_cache!()

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

enable_query_cache!()

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

query_cache()

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 217
def query_cache
  if @pinned && @owner != ActiveSupport::IsolatedExecutionState.context
    # With transactional tests, if the connection is pinned, any thread
    # other than the one that pinned the connection need to go through the
    # query cache pool, so each thread get a different cache.
    pool.query_cache
  else
    @query_cache
  end
end

query_cache_enabled()

# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 228
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 245
def uncached(dirties: true, &block)
  pool.disable_query_cache(dirties: dirties, &block)
end