Class: Lutaml::Model::CachedTypeResolver Private
- Inherits:
-
Object
- Object
- Lutaml::Model::CachedTypeResolver
- Defined in:
- lib/lutaml/model/cached_type_resolver.rb,
lib/lutaml/model/cached_type_resolver/mutex_hash_cache.rb,
lib/lutaml/model/cached_type_resolver/concurrent_map_cache.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
CachedTypeResolver adds caching to any TypeResolver using the Decorator pattern.
This is an INTERNAL class. Users should use Register and GlobalRegister.
Responsibility: Add caching to type resolution
This class:
- Decorates any TypeResolver-like object (duck typing)
- Uses a runtime-selected cache backend
- Centralized cache management - ONE place to clear ALL type caches
Defined Under Namespace
Classes: ConcurrentMapCache, MutexHashCache
Instance Attribute Summary collapse
-
#cache_backend ⇒ Object
readonly
private
The delegate resolver (typically TypeResolver).
-
#delegate ⇒ Object
readonly
private
The delegate resolver (typically TypeResolver).
Class Method Summary collapse
- .default_cache_backend ⇒ Object private
Instance Method Summary collapse
-
#cache_stats ⇒ Hash
private
Get cache statistics (useful for debugging/monitoring).
-
#clear_all_caches ⇒ void
private
Clear all caches.
-
#clear_cache(context_id) ⇒ void
private
Clear the cache for a specific context.
-
#initialize(delegate:, cache_backend: self.class.default_cache_backend) ⇒ CachedTypeResolver
constructor
private
Create a new CachedTypeResolver.
-
#resolvable?(name, context) ⇒ Boolean
private
Check if a type can be resolved, using cache if available.
-
#resolve(name, context, materialize: true) ⇒ Class
private
Resolve a type name to a class, using cache if available.
-
#resolve_or_nil(name, context) ⇒ Class?
private
Try to resolve a type, returning nil if not found.
Constructor Details
#initialize(delegate:, cache_backend: self.class.default_cache_backend) ⇒ CachedTypeResolver
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new CachedTypeResolver.
51 52 53 54 |
# File 'lib/lutaml/model/cached_type_resolver.rb', line 51 def initialize(delegate:, cache_backend: self.class.default_cache_backend) @delegate = delegate @cache_backend = cache_backend end |
Instance Attribute Details
#cache_backend ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The delegate resolver (typically TypeResolver).
36 37 38 |
# File 'lib/lutaml/model/cached_type_resolver.rb', line 36 def cache_backend @cache_backend end |
#delegate ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The delegate resolver (typically TypeResolver).
36 37 38 |
# File 'lib/lutaml/model/cached_type_resolver.rb', line 36 def delegate @delegate end |
Class Method Details
.default_cache_backend ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
38 39 40 41 42 43 44 |
# File 'lib/lutaml/model/cached_type_resolver.rb', line 38 def self.default_cache_backend if Lutaml::Model.opal? MutexHashCache.new else ConcurrentMapCache.new end end |
Instance Method Details
#cache_stats ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get cache statistics (useful for debugging/monitoring).
125 126 127 128 129 130 131 132 |
# File 'lib/lutaml/model/cached_type_resolver.rb', line 125 def cache_stats keys = @cache_backend.keys { size: keys.size, keys: keys, } end |
#clear_all_caches ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Clear all caches.
118 119 120 |
# File 'lib/lutaml/model/cached_type_resolver.rb', line 118 def clear_all_caches @cache_backend.clear end |
#clear_cache(context_id) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Clear the cache for a specific context.
111 112 113 |
# File 'lib/lutaml/model/cached_type_resolver.rb', line 111 def clear_cache(context_id) @cache_backend.clear_context(context_id) end |
#resolvable?(name, context) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if a type can be resolved, using cache if available.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/lutaml/model/cached_type_resolver.rb', line 84 def resolvable?(name, context) cache_key = if name.is_a?(Class) [context.id, name] else build_cache_key(name, context) end return true if @cache_backend.key?(cache_key) @delegate.resolvable?(name, context) end |
#resolve(name, context, materialize: true) ⇒ Class
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolve a type name to a class, using cache if available.
Cache backends synchronize storage, but compute outside exclusive cache updates so recursive type resolution can populate related keys.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lutaml/model/cached_type_resolver.rb', line 65 def resolve(name, context, materialize: true) return @delegate.resolve(name, context, materialize: false) unless materialize cache_key = if name.is_a?(Class) [context.id, name] else build_cache_key(name, context) end @cache_backend.fetch_or_store(cache_key) do @delegate.resolve(name, context) end end |
#resolve_or_nil(name, context) ⇒ Class?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Try to resolve a type, returning nil if not found.
101 102 103 104 105 |
# File 'lib/lutaml/model/cached_type_resolver.rb', line 101 def resolve_or_nil(name, context) resolve(name, context) rescue UnknownTypeError nil end |