Module: SecondLevelCache::ActiveRecord::Associations::Preloader::Association::LoaderQuery

Defined in:
lib/second_level_cache/active_record/preloader/association.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



17
18
19
# File 'lib/second_level_cache/active_record/preloader/association.rb', line 17

def association
  @association
end

Instance Method Details

#initialize(association, scope, association_key_name) ⇒ Object



21
22
23
24
25
# File 'lib/second_level_cache/active_record/preloader/association.rb', line 21

def initialize(association, scope, association_key_name)
  @association = association
  @scope = scope
  @association_key_name = association_key_name
end

#load_records_for_keys(keys, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/second_level_cache/active_record/preloader/association.rb', line 31

def load_records_for_keys(keys, &block)
  ids = keys.to_a

  return super unless klass.second_level_cache_enabled?
  return super unless reflection.is_a?(::ActiveRecord::Reflection::BelongsToReflection)
  return super if klass.default_scopes.present? || reflection.scope
  return super if association_key_name.to_s != klass.primary_key

  map_cache_keys = ids.map { |id| klass.second_level_cache_key(id) }
  records_from_cache = ::SecondLevelCache.cache_store.read_multi(*map_cache_keys)
  record_marshals = RecordMarshal.load_multi(records_from_cache.values, &block)

  # NOTICE
  # Rails.cache.read_multi return hash that has keys only hitted.
  # eg. Rails.cache.read_multi(1,2,3) => {2 => hit_value, 3 => hit_value}
  hitted_ids = record_marshals.map { |record| record.read_attribute(association_key_name).to_s }
  missed_ids = ids.map(&:to_s) - hitted_ids
  ActiveSupport::Notifications.instrument("preload.second_level_cache", key: association_key_name, hit: hitted_ids, miss: missed_ids)
  return SecondLevelCache::RecordRelation.new(record_marshals) if missed_ids.empty?

  records_from_db = super(missed_ids.to_set, &block)
  records_from_db.map { |r| r.write_second_level_cache }

  SecondLevelCache::RecordRelation.new(records_from_db + record_marshals)
end

#reflectionObject



27
28
29
# File 'lib/second_level_cache/active_record/preloader/association.rb', line 27

def reflection
  association.send(:reflection)
end