Raxol.Performance.Caches.CacheHelper (Raxol v2.6.0)

View Source

Shared cache get-or-compute pattern and telemetry for performance caches.

Eliminates the repeated pattern of checking ETS cache, emitting hit/miss telemetry, computing on miss, and storing the result.

Summary

Functions

Gets a value from cache or computes and caches it.

Emits a telemetry event with a standard %{count: 1} measurement.

Functions

cache_through(key, telemetry_prefix, telemetry_metadata, compute_fn)

@spec cache_through(String.t(), [atom()], map(), (-> term())) :: term()

Gets a value from cache or computes and caches it.

On cache hit, emits a :hit telemetry event and returns the cached value. On cache miss, emits a :miss event, calls compute_fn, caches the result, and returns it.

Parameters

  • key - The cache key string
  • telemetry_prefix - e.g. [:raxol, :performance, :font_metrics_cache]
  • telemetry_metadata - e.g. %{key_type: :char_width}
  • compute_fn - Zero-arity function that computes the value on miss

emit_telemetry(prefix, event, metadata)

@spec emit_telemetry([atom()], atom(), map()) :: :ok

Emits a telemetry event with a standard %{count: 1} measurement.