Anvil.ForgeBridge.Cached (Anvil v0.1.1)

View Source

Caching wrapper for ForgeBridge backends.

Adds TTL-based caching layer on top of any ForgeBridge backend to improve performance and reduce load on Forge. Best for hybrid deployments where sample content is relatively stable.

Configuration

# config/config.exs
config :anvil,
  forge_bridge_backend: Anvil.ForgeBridge.Cached,
  forge_bridge_primary_backend: Anvil.ForgeBridge.Direct,
  forge_cache_ttl: :timer.minutes(15)

Features

  • TTL-based expiration (default 15 minutes)
  • Cache warming for hot queues
  • Graceful degradation (serve stale on backend failure)
  • Telemetry events for cache hits/misses

Cache Invalidation

Subscribe to sample update events for proactive invalidation:

Phoenix.PubSub.subscribe(Forge.PubSub, "sample_updates")

def handle_info({:sample_updated, sample_id}, state) do
  Anvil.ForgeBridge.Cached.invalidate(sample_id)
  {:noreply, state}
end

Summary

Functions

Clears the entire sample cache.

Invalidates a cached sample by ID.

Warms the cache for a list of sample IDs.

Functions

clear_cache()

@spec clear_cache() :: {:ok, integer()}

Clears the entire sample cache.

invalidate(sample_id)

@spec invalidate(binary()) :: :ok

Invalidates a cached sample by ID.

warm_cache(sample_ids)

@spec warm_cache([binary()]) :: :ok

Warms the cache for a list of sample IDs.

Useful for preloading samples for a queue before labelers request them.