跳至内容 跳至搜索
命名空间
方法
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