Raxol.Terminal.ImageCache (Raxol Terminal v2.6.0)

Copy Markdown View Source

ETS-backed cache for decoded and encoded terminal images.

Caches expensive operations:

  • PNG decode results (raw pixel data)
  • Sixel/Kitty encoded output for given parameters

Keys are {content_hash, opts_hash} tuples. Entries expire after a configurable TTL (default 5 minutes). The cache is created lazily on first access and owned by the calling process (or an explicit owner).

Usage

ImageCache.start()
ImageCache.put("img.png", png_bytes, %{max_colors: 64})
{:ok, cached} = ImageCache.get("img.png", %{max_colors: 64})
ImageCache.evict("img.png")

Summary

Functions

Deletes all entries from the cache.

Removes all entries matching a source identifier (any opts).

Fetches from cache or computes and caches the value.

Retrieves a cached value. Returns {:ok, value} or :miss. Expired entries are transparently deleted.

Removes all expired entries from the cache.

Stores a value in the cache keyed by source identifier and options.

Returns the number of entries currently in the cache.

Creates the ETS table if it doesn't already exist. Safe to call multiple times.

Types

cache_entry()

@type cache_entry() :: {cache_key(), term(), integer()}

cache_key()

@type cache_key() :: {binary(), binary()}

Functions

clear()

@spec clear() :: :ok

Deletes all entries from the cache.

evict(source_id)

@spec evict(binary()) :: :ok

Removes all entries matching a source identifier (any opts).

fetch(source_id, opts, compute_fn)

@spec fetch(binary(), map(), (-> {:ok, term()} | {:error, term()})) ::
  {:ok, term()} | {:error, term()}

Fetches from cache or computes and caches the value.

The compute_fn is called only on cache miss and must return {:ok, value} or {:error, reason}.

get(source_id, opts \\ %{})

@spec get(binary(), map()) :: {:ok, term()} | :miss

Retrieves a cached value. Returns {:ok, value} or :miss. Expired entries are transparently deleted.

prune()

@spec prune() :: non_neg_integer()

Removes all expired entries from the cache.

put(source_id, value, opts \\ %{})

@spec put(binary(), term(), map()) :: :ok

Stores a value in the cache keyed by source identifier and options.

size()

@spec size() :: non_neg_integer()

Returns the number of entries currently in the cache.

start()

@spec start() :: :ok

Creates the ETS table if it doesn't already exist. Safe to call multiple times.