GnuplotEx.LiveView.Cache (gnuplot_ex v0.5.1)

ETS-based cache for rendered plots with TTL support.

Prevents redundant gnuplot executions when plot data hasn't changed. Thread-safe for concurrent LiveView processes.

Usage

The cache is automatically started by the application supervision tree when Phoenix LiveView is available.

You typically don't need to interact with this module directly - the GnuplotEx.LiveView.Component handles caching automatically.

Manual Usage

# Get cached plot output
case GnuplotEx.LiveView.Cache.get(cache_key) do
  {:ok, output} -> output
  :miss -> # render plot
end

# Store plot output with TTL
GnuplotEx.LiveView.Cache.put(cache_key, output, 60_000)

# Get cache statistics
stats = GnuplotEx.LiveView.Cache.stats()
# => %{entries: 42, memory_bytes: 1_048_576}

# Clear all cache entries
GnuplotEx.LiveView.Cache.clear()

Configuration

# config/config.exs
config :gnuplot_ex,
  cache_cleanup_interval: 60_000  # milliseconds

Summary

Functions

Returns a specification to start this module under a supervisor.

Clear all cache entries.

Get cached plot output.

Store plot output with TTL.

Starts the cache GenServer.

Get cache statistics.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear()

@spec clear() :: :ok

Clear all cache entries.

Examples

iex> GnuplotEx.LiveView.Cache.clear()
:ok

get(key)

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

Get cached plot output.

Returns {:ok, output} if found and not expired, :miss otherwise.

Examples

iex> GnuplotEx.LiveView.Cache.get(:my_key)
{:ok, "<svg>...</svg>"}

iex> GnuplotEx.LiveView.Cache.get(:nonexistent)
:miss

put(key, output, ttl_ms)

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

Store plot output with TTL.

Examples

iex> GnuplotEx.LiveView.Cache.put(:my_key, "<svg>...</svg>", 60_000)
:ok

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the cache GenServer.

This is typically called by the application supervision tree.

stats()

@spec stats() :: %{entries: non_neg_integer(), memory_bytes: non_neg_integer()}

Get cache statistics.

Returns a map with:

  • :entries - Number of cached items
  • :memory_bytes - Approximate memory usage in bytes

Examples

iex> GnuplotEx.LiveView.Cache.stats()
%{entries: 42, memory_bytes: 1_048_576}