Class: Lutaml::Model::CachedTypeResolver Private

Inherits:
Object
  • Object
show all
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

Examples:

Basic usage

resolver = CachedTypeResolver.new(delegate: TypeResolver)
resolver.resolve(:string, context)  # First call - resolves and caches
resolver.resolve(:string, context)  # Second call - returns cached

Clearing caches

resolver.clear_cache(:my_context)  # Clear cache for specific context
resolver.clear_all_caches          # Clear all caches

Defined Under Namespace

Classes: ConcurrentMapCache, MutexHashCache

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • delegate (Object)

    Any object responding to #resolve(name, context)

  • cache_backend (Object) (defaults to: self.class.default_cache_backend)

    Any object implementing the resolver cache interface used internally by 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_backendObject (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).

Returns:

  • (Object)

    The delegate resolver (typically TypeResolver)



36
37
38
# File 'lib/lutaml/model/cached_type_resolver.rb', line 36

def cache_backend
  @cache_backend
end

#delegateObject (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).

Returns:

  • (Object)

    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_backendObject

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 RuntimeCompatibility.opal?
    MutexHashCache.new
  else
    ConcurrentMapCache.new
  end
end

Instance Method Details

#cache_statsHash

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).

Returns:

  • (Hash)

    Cache statistics



123
124
125
126
127
128
129
130
# File 'lib/lutaml/model/cached_type_resolver.rb', line 123

def cache_stats
  keys = @cache_backend.keys

  {
    size: keys.size,
    keys: keys,
  }
end

#clear_all_cachesvoid

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.



116
117
118
# File 'lib/lutaml/model/cached_type_resolver.rb', line 116

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.

Parameters:

  • context_id (Symbol)

    The context ID to clear caches for



109
110
111
# File 'lib/lutaml/model/cached_type_resolver.rb', line 109

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.

Parameters:

  • name (Symbol, String, Class)

    The type name or class to check

  • context (TypeContext)

    The resolution context

Returns:

  • (Boolean)

    true if type can be resolved



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lutaml/model/cached_type_resolver.rb', line 82

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) ⇒ 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.

Parameters:

  • name (Symbol, String, Class)

    The type name or class to resolve

  • context (TypeContext)

    The resolution context

Returns:

  • (Class)

    The resolved type class

Raises:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lutaml/model/cached_type_resolver.rb', line 65

def resolve(name, context)
  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.

Parameters:

  • name (Symbol, String, Class)

    The type name or class to resolve

  • context (TypeContext)

    The resolution context

Returns:

  • (Class, nil)

    The resolved type class or nil



99
100
101
102
103
# File 'lib/lutaml/model/cached_type_resolver.rb', line 99

def resolve_or_nil(name, context)
  resolve(name, context)
rescue UnknownTypeError
  nil
end