AshScylla.PreparedStatementCache (AshScylla v0.4.0)

Copy Markdown View Source

ETS-based prepared statement cache for ScyllaDB/Cassandra queries.

Caches prepared statements keyed by query hash to eliminate repeated query parsing overhead on ScyllaDB. This is especially impactful for high-throughput workloads where the same queries are executed repeatedly.

All ETS operations are routed through the GenServer to avoid race conditions when multiple processes access the cache concurrently.

Usage

AshScylla.PreparedStatementCache.prepare(repo, "SELECT * FROM users WHERE id = ?")

Starting the Cache

Add to your supervision tree:

children = [
  AshScylla.PreparedStatementCache,
  # ... other children
]

Or start manually:

AshScylla.PreparedStatementCache.start_link([])

Summary

Functions

Returns a specification to start this module under a supervisor.

Clears all cached prepared statements.

Invalidates a specific cached statement by CQL string.

Prepares a CQL statement, using the cache if available.

Returns the number of cached statements.

Starts the prepared statement cache.

Returns the ETS table tid for inspection/testing.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear()

@spec clear() :: :ok

Clears all cached prepared statements.

invalidate(cql)

@spec invalidate(String.t()) :: :ok

Invalidates a specific cached statement by CQL string.

prepare(repo, cql, opts \\ [])

@spec prepare(module(), String.t(), keyword()) :: {:ok, term()} | {:error, term()}

Prepares a CQL statement, using the cache if available.

Returns {:ok, stmt} on success or {:error, reason} on failure.

size()

@spec size() :: non_neg_integer()

Returns the number of cached statements.

start_link(opts \\ [])

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

Starts the prepared statement cache.

When no :name option is given, the GenServer is registered globally as {:global, __MODULE__} so that all processes share a single cache. Pass name: :undefined or a custom name to register locally instead.

table()

@spec table() :: :ets.tid() | nil

Returns the ETS table tid for inspection/testing.