Class: ViewModel::ActiveRecord::Cache
- Inherits:
-
Object
- Object
- ViewModel::ActiveRecord::Cache
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
Class Method Summary
collapse
Instance Method Summary
collapse
-
#cache_for(migration_version) ⇒ Object
-
#cache_version ⇒ Object
-
#clear ⇒ Object
-
#delete(*ids) ⇒ Object
Handles being called with either ids or model/viewmodel objects.
-
#fetch(ids, initial_viewmodels: nil, migration_versions: {}, locked: false, serialize_context: @viewmodel_class.new_serialize_context) ⇒ Object
deprecated
Deprecated.
Replaced by class methods
-
#fetch_by_viewmodel(viewmodels, migration_versions: {}, locked: false, serialize_context: @viewmodel_class.new_serialize_context) ⇒ Object
deprecated
Deprecated.
Replaced by class methods
-
#id_for(key) ⇒ Object
-
#initialize(viewmodel_class, cache_group: nil) ⇒ Cache
constructor
If cache_group: is specified, it must be a group of a single key: ‘:id`.
-
#key_for(id, migration_version) ⇒ Object
-
#load(ids, migration_version) ⇒ Object
-
#migrated_cache_version(migration_versions) ⇒ Object
-
#store(id, migration_version, data_serialization, ref_cache) ⇒ Object
Save the provided serialization and reference data in the cache.
Constructor Details
#initialize(viewmodel_class, cache_group: nil) ⇒ Cache
If cache_group: is specified, it must be a group of a single key: ‘:id`
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/view_model/active_record/cache.rb', line 39
def initialize(viewmodel_class, cache_group: nil)
@viewmodel_class = viewmodel_class
@cache_group = cache_group || create_default_cache_group
@migrated_cache_group = @cache_group.register_child_group(:migrated, :version)
@cache = @cache_group.register_cache(cache_name)
@migrated_cache = @migrated_cache_group.register_cache(cache_name)
end
|
Instance Attribute Details
#viewmodel_class ⇒ Object
Returns the value of attribute viewmodel_class.
12
13
14
|
# File 'lib/view_model/active_record/cache.rb', line 12
def viewmodel_class
@viewmodel_class
end
|
Class Method Details
.render_from_cache(viewmodel_class, ids, initial_viewmodels: nil, migration_versions: {}, locked: false, serialize_context: viewmodel_class.new_serialize_context) ⇒ Object
32
33
34
35
|
# File 'lib/view_model/active_record/cache.rb', line 32
def render_from_cache(viewmodel_class, ids, initial_viewmodels: nil, migration_versions: {}, locked: false, serialize_context: viewmodel_class.new_serialize_context)
worker = CacheWorker.new(migration_versions: migration_versions, serialize_context: serialize_context)
worker.render_from_cache(viewmodel_class, ids, initial_viewmodels: initial_viewmodels, locked: locked)
end
|
.render_viewmodels_from_cache(viewmodels, migration_versions: {}, locked: false, serialize_context: nil) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/view_model/active_record/cache.rb', line 15
def render_viewmodels_from_cache(viewmodels, migration_versions: {}, locked: false, serialize_context: nil)
if viewmodels.empty?
return [], {}
end
ids = viewmodels.map(&:id)
viewmodel_class = viewmodels.first.class
serialize_context ||= viewmodel_class.new_serialize_context
render_from_cache(viewmodel_class, ids,
initial_viewmodels: viewmodels,
migration_versions: migration_versions,
locked: locked,
serialize_context: serialize_context)
end
|
Instance Method Details
#cache_for(migration_version) ⇒ Object
318
319
320
321
322
323
324
|
# File 'lib/view_model/active_record/cache.rb', line 318
def cache_for(migration_version)
if migration_version
@migrated_cache
else
@cache
end
end
|
#cache_version ⇒ Object
350
351
352
353
354
355
356
|
# File 'lib/view_model/active_record/cache.rb', line 350
def cache_version
@cache_version ||=
begin
versions = @viewmodel_class.deep_schema_version(include_referenced: false)
ViewModel.schema_hash(versions)
end
end
|
#clear ⇒ Object
62
63
64
|
# File 'lib/view_model/active_record/cache.rb', line 62
def clear
@cache_group.invalidate_cache_group
end
|
#delete(*ids) ⇒ Object
Handles being called with either ids or model/viewmodel objects
53
54
55
56
57
58
59
60
|
# File 'lib/view_model/active_record/cache.rb', line 53
def delete(*ids)
ids.each do |id|
id = id.id if id.respond_to?(:id)
raise ArgumentError.new unless id.is_a?(Numeric) || id.is_a?(String)
@cache_group.delete_all(@cache.key.new(id))
end
end
|
#fetch(ids, initial_viewmodels: nil, migration_versions: {}, locked: false, serialize_context: @viewmodel_class.new_serialize_context) ⇒ Object
Deprecated.
Replaced by class methods
73
74
75
76
77
|
# File 'lib/view_model/active_record/cache.rb', line 73
def fetch(ids, initial_viewmodels: nil, migration_versions: {}, locked: false, serialize_context: @viewmodel_class.new_serialize_context)
self.class.render_from_cache(@viewmodel_class, ids,
initial_viewmodels: initial_viewmodels, locked: locked,
migration_versions: migration_versions, serialize_context: serialize_context)
end
|
#fetch_by_viewmodel(viewmodels, migration_versions: {}, locked: false, serialize_context: @viewmodel_class.new_serialize_context) ⇒ Object
Deprecated.
Replaced by class methods
67
68
69
70
|
# File 'lib/view_model/active_record/cache.rb', line 67
def fetch_by_viewmodel(viewmodels, migration_versions: {}, locked: false, serialize_context: @viewmodel_class.new_serialize_context)
ids = viewmodels.map(&:id)
fetch(ids, initial_viewmodels: viewmodels, migration_versions: migration_versions, locked: locked, serialize_context: serialize_context)
end
|
#id_for(key) ⇒ Object
334
335
336
|
# File 'lib/view_model/active_record/cache.rb', line 334
def id_for(key)
key[:id]
end
|
#key_for(id, migration_version) ⇒ Object
326
327
328
329
330
331
332
|
# File 'lib/view_model/active_record/cache.rb', line 326
def key_for(id, migration_version)
if migration_version
@migrated_cache.key.new(id, migration_version)
else
@cache.key.new(id)
end
end
|
#load(ids, migration_version) ⇒ Object
344
345
346
347
348
|
# File 'lib/view_model/active_record/cache.rb', line 344
def load(ids, migration_version)
keys = ids.map { |id| key_for(id, migration_version) }
results = cache_for(migration_version).read_multi(keys)
results.transform_keys! { |key| id_for(key) }
end
|
#migrated_cache_version(migration_versions) ⇒ Object
358
359
360
361
362
363
364
365
366
367
368
|
# File 'lib/view_model/active_record/cache.rb', line 358
def migrated_cache_version(migration_versions)
versions = ViewModel::Migrator.migrated_deep_schema_version(viewmodel_class, migration_versions, include_referenced: false)
version_hash = ViewModel.schema_hash(versions)
if version_hash == cache_version
nil
else
version_hash
end
end
|
#store(id, migration_version, data_serialization, ref_cache) ⇒ Object
Save the provided serialization and reference data in the cache
339
340
341
342
|
# File 'lib/view_model/active_record/cache.rb', line 339
def store(id, migration_version, data_serialization, ref_cache)
key = key_for(id, migration_version)
cache_for(migration_version).write(key, { data: data_serialization, ref_cache: ref_cache })
end
|