Module: Lutaml::Model::Serialize::ConversionCaching
- Included in:
- ClassMethods
- Defined in:
- lib/lutaml/model/serialize/conversion_caching.rb
Overview
Opt-in caching of format conversions in both directions: deserialized whole objects and serialized output strings (issue #267).
A class declares cache_conversions; plain from_<format> and
to_<format> calls are then served from the store configured via
Config.conversion_cache. The store is duck-typed — anything
responding to get(key) and set(key, value) works.
Lutaml::Store::BasicStore (lutaml-store gem) is the recommended
backend; TTL, eviction, persistence and clearing are the store's
concern, and store exceptions propagate unchanged. Cache hits do
not emit Instrumentation events — the work did not happen.
Semantics (deliberate):
from_*hits return the same cached instance for identical input — across callers and threads. Treat results as read-only; classes whose callers mutate parse results must not opt in.- Keys digest everything that determines the result: the input string (:from) or instance (:to), plus all options except :register (folded into the key as a resolved id). What cannot be digested bypasses instead of risking a wrong hit: non-String inputs (Pathname/IO — content lives elsewhere), non-Hash options (Psych/JSON generator protocol objects), and graphs Marshal refuses — e.g. instances parsed from XML hold native parser nodes for round-trip fidelity, so :to caching engages for programmatically built instances.
- Structural invalidation is not propagated: mutating a register's mappings or calling GlobalContext.clear_caches does not touch the store — clear or replace the store after such mutations.
- A hit returns before the wrapped body runs, so option-hash mutations the body performs on a miss (e.g. consuming :adapter) do not happen on a hit.
- Caching is disabled under Opal: keys need native Marshal/Digest.
Constant Summary collapse
- NATIVE_RUNTIME =
!Lutaml::Model::RuntimeCompatibility.opal?
- UNMARSHALABLE_IVAR =
:@__lutaml_conversion_cache_unmarshalable
Instance Method Summary collapse
Instance Method Details
#cache_conversions ⇒ Object
46 47 48 |
# File 'lib/lutaml/model/serialize/conversion_caching.rb', line 46 def cache_conversions define_singleton_method(:conversion_caching_enabled?) { true } end |
#conversion_caching_enabled? ⇒ Boolean
50 51 52 |
# File 'lib/lutaml/model/serialize/conversion_caching.rb', line 50 def conversion_caching_enabled? false end |