Class: IdentityCache::Cached::PrimaryIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/identity_cache/cached/primary_index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ PrimaryIndex

Returns a new instance of PrimaryIndex.



8
9
10
# File 'lib/identity_cache/cached/primary_index.rb', line 8

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/identity_cache/cached/primary_index.rb', line 6

def model
  @model
end

Instance Method Details

#cache_decode(cache_value) ⇒ Object



77
78
79
# File 'lib/identity_cache/cached/primary_index.rb', line 77

def cache_decode(cache_value)
  Encoder.decode(cache_value, model)
end

#cache_encode(record) ⇒ Object



73
74
75
# File 'lib/identity_cache/cached/primary_index.rb', line 73

def cache_encode(record)
  Encoder.encode(record)
end

#cache_key(id) ⇒ Object



51
52
53
# File 'lib/identity_cache/cached/primary_index.rb', line 51

def cache_key(id)
  "#{model.rails_cache_key_namespace}#{cache_key_prefix}#{id}"
end

#expire(id) ⇒ Object



46
47
48
49
# File 'lib/identity_cache/cached/primary_index.rb', line 46

def expire(id)
  id = cast_id(id)
  IdentityCache.cache.delete(cache_key(id))
end

#fetch(id, cache_fetcher_options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/identity_cache/cached/primary_index.rb', line 12

def fetch(id, cache_fetcher_options)
  id = cast_id(id)
  return unless id

  record = if model.should_use_cache?
    object = CacheKeyLoader.load(self, id, cache_fetcher_options)
    if object && object.id != id
      IdentityCache.logger.error(
        <<~MSG.squish
          [IDC id mismatch] fetch_by_id_requested=#{id}
          fetch_by_id_got=#{object.id}
          for #{object.inspect[(0..100)]}
        MSG
      )
    end
    object
  else
    load_one_from_db(id)
  end
  record
end

#fetch_multi(ids) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/identity_cache/cached/primary_index.rb', line 34

def fetch_multi(ids)
  ids.map! { |id| cast_id(id) }.compact!
  id_to_record_hash = if model.should_use_cache?
    id_to_record_hash = CacheKeyLoader.load_multi(self, ids)
  else
    load_multi_from_db(ids)
  end
  records = ids.map { |id| id_to_record_hash[id] }
  records.compact!
  records
end

#load_multi_from_db(ids) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/identity_cache/cached/primary_index.rb', line 64

def load_multi_from_db(ids)
  return {} if ids.empty?

  records = build_query(ids).to_a
  model.send(:setup_embedded_associations_on_miss, records)
  records.each { |record| record.send(:mark_as_loaded_by_idc) }
  records.index_by(&:id)
end

#load_one_from_db(id) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/identity_cache/cached/primary_index.rb', line 55

def load_one_from_db(id)
  record = build_query(id).take
  if record
    model.send(:setup_embedded_associations_on_miss, [record])
    record.send(:mark_as_loaded_by_idc)
  end
  record
end