Class: ViewModel::ActiveRecord::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/view_model/active_record/cache.rb

Overview

Cache for ViewModels that wrap ActiveRecord models.

Defined Under Namespace

Modules: CacheableView Classes: CacheWorker, UncacheableViewModelError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(viewmodel_class, cache_group: nil) ⇒ Cache

If cache_group: is specified, it must be a group of a single key: ‘:id`



14
15
16
17
18
# File 'lib/view_model/active_record/cache.rb', line 14

def initialize(viewmodel_class, cache_group: nil)
  @viewmodel_class = viewmodel_class
  @cache_group = cache_group || create_default_cache_group # requires @viewmodel_class
  @cache = @cache_group.register_cache(cache_name)
end

Instance Attribute Details

#viewmodel_classObject (readonly)

Returns the value of attribute viewmodel_class.



11
12
13
# File 'lib/view_model/active_record/cache.rb', line 11

def viewmodel_class
  @viewmodel_class
end

Instance Method Details

#clearObject



26
27
28
# File 'lib/view_model/active_record/cache.rb', line 26

def clear
  @cache_group.invalidate_cache_group
end

#delete(*ids) ⇒ Object



20
21
22
23
24
# File 'lib/view_model/active_record/cache.rb', line 20

def delete(*ids)
  ids.each do |id|
    @cache_group.delete_all(key_for(id))
  end
end

#fetch(ids, initial_viewmodels: nil, locked: false, serialize_context: @viewmodel_class.new_serialize_context) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/view_model/active_record/cache.rb', line 35

def fetch(ids, initial_viewmodels: nil, locked: false, serialize_context: @viewmodel_class.new_serialize_context)
  data_serializations = Array.new(ids.size)
  worker = CacheWorker.new(serialize_context: serialize_context)

  # If initial root viewmodels were provided, visit them to ensure that they
  # are visible. Other than this, no traversal callbacks are performed, as a
  # view may be resolved from the cache without ever loading its viewmodel.
  # Note that if unlocked, these views will be reloaded as part of obtaining a
  # share lock. If the visibility of this viewmodel can change due to edits,
  # it is necessary to obtain a lock before calling `fetch`.
  initial_viewmodels&.each do |v|
    serialize_context.run_callback(ViewModel::Callbacks::Hook::BeforeVisit, v)
    serialize_context.run_callback(ViewModel::Callbacks::Hook::AfterVisit, v)
  end

  # Collect input array positions for each id, allowing duplicates
  positions = ids.each_with_index.with_object({}) do |(id, i), h|
    (h[id] ||= []) << i
  end

  # Fetch duplicates only once
  ids = positions.keys

  # Load existing serializations from the cache
  cached_serializations = worker.load_from_cache(self, ids)
  cached_serializations.each do |id, data|
    positions[id].each do |idx|
      data_serializations[idx] = data
    end
  end

  # Resolve and serialize missing views
  missing_ids = ids.to_set.subtract(cached_serializations.keys)

  # If initial viewmodels have been locked, we can serialize them for cache
  #  misses.
  available_viewmodels =
    if locked
      initial_viewmodels&.each_with_object({}) do |vm, h|
        h[vm.id] = vm if missing_ids.include?(vm.id)
      end
    end

  @viewmodel_class.transaction do
    # Load remaining views and serialize
    viewmodels = worker.find_and_preload_viewmodels(@viewmodel_class, missing_ids.to_a,
                                                    available_viewmodels: available_viewmodels)

    loaded_serializations = worker.serialize_and_cache(viewmodels)
    loaded_serializations.each do |id, data|
      positions[id].each do |idx|
        data_serializations[idx] = data
      end
    end

    # Resolve references
    worker.resolve_references!

    return data_serializations, worker.resolved_references
  end
end

#fetch_by_viewmodel(viewmodels, locked: false, serialize_context: @viewmodel_class.new_serialize_context) ⇒ Object



30
31
32
33
# File 'lib/view_model/active_record/cache.rb', line 30

def fetch_by_viewmodel(viewmodels, locked: false, serialize_context: @viewmodel_class.new_serialize_context)
  ids = viewmodels.map(&:id)
  fetch(ids, initial_viewmodels: viewmodels, locked: false, serialize_context: serialize_context)
end

#id_for(key) ⇒ Object



232
233
234
# File 'lib/view_model/active_record/cache.rb', line 232

def id_for(key)
  key[:id]
end

#key_for(id) ⇒ Object



228
229
230
# File 'lib/view_model/active_record/cache.rb', line 228

def key_for(id)
  cache.key.new(id)
end

#load(ids, serialize_context:) ⇒ Object



241
242
243
244
245
# File 'lib/view_model/active_record/cache.rb', line 241

def load(ids, serialize_context:)
  keys = ids.map { |id| key_for(id) }
  results = cache.read_multi(keys)
  results.transform_keys! { |key| id_for(key) }
end

#store(id, data_serialization, ref_cache, serialize_context:) ⇒ Object

Save the provided serialization and reference data in the cache



237
238
239
# File 'lib/view_model/active_record/cache.rb', line 237

def store(id, data_serialization, ref_cache, serialize_context:)
  cache.write(key_for(id), { data: data_serialization, ref_cache: ref_cache })
end