kura_query_cache (kura v2.31.3)
View SourceETS-based cache for compiled query results.
Caches compiled query shapes for kura_query_compiler:to_sql_cached/2,
keyed by {RepoMod, Tenant, Shape} so two repos with different dialects
don't share entries. A shape is a query with its bound values replaced by
markers (see kura_query_shape), so queries that differ only in their
values share one entry.
The key carries the whole shape term rather than a hash of it: an entry holds SQL and a parameter template, so serving the wrong entry would hand a caller another query's rows rather than merely a slower path. ETS hashes the term itself, so hashing the key by hand only reintroduces a collision class the table had already eliminated.
The table is bounded by the number of distinct shapes, plus one entry per
value set for any condition kura_query_shape leaves in the shape. Two
limits bound it regardless, both counted in words and both configurable:
query_cache_max_memory(default 8000000 words, ~64 MB on 64-bit) - once the table exceeds it the whole cache is dropped and refills.query_cache_max_entry_size(default 4096 words) - a single entry bigger than this is never interned.
Both limits count the bytes of every off-heap binary an entry holds,
such as its SQL text. A term's flat size leaves those bytes out, and so
does ets:info(Tab, memory).
An entry is served only by the release of kura that stored it. Each one
carries a format version, and an entry with another version, with none, or
whose parts are not what the compiler stores, is dropped when it is read
and compiled again. The version covers what an entry holds; it does not
notice a schema module whose code changed. After a hot code upgrade of
kura or of a schema, call flush/0, and kura_schema:forget/1 for each
schema that changed.
An in list is one shape per list length, so a caller-supplied array
of arbitrary length still interns an entry per length. The per-entry
ceiling keeps those entries small.
The ETS table is owned by kura_query_cache_owner (a gen_server under
kura_sup), so the table survives any caller exiting.
Summary
Types
A compiled shape's SQL, its parameter template and what the compiler worked out about the shape, or a shape that cannot be cached.
Functions
Drop every cached entry. Required after swapping a repo's dialect at runtime, since the dialect is not part of the cache key. Also used by tests.
Initialize the query cache ETS table. No-op when the cache owner is already running (the normal app-startup path). Used by tests that exercise the cache without starting the kura app.
Types
-type entry() :: {binary(), [term()], kura_query_compiler:facts()} | uncacheable.
A compiled shape's SQL, its parameter template and what the compiler worked out about the shape, or a shape that cannot be cached.
Functions
-spec flush() -> ok.
Drop every cached entry. Required after swapping a repo's dialect at runtime, since the dialect is not part of the cache key. Also used by tests.
-spec init() -> ok.
Initialize the query cache ETS table. No-op when the cache owner is already running (the normal app-startup path). Used by tests that exercise the cache without starting the kura app.
-spec init([]) -> {ok, []}.
-spec start_link() -> gen_server:start_ret().