Each connection owns a statement_cache that reuses sqlite3_stmt*
handles keyed by SQL text. This reduces parse/prepare overhead for frequently executed
statements.
Use cases
- LRU eviction keeps the cache bounded while still prioritizing hot statements.
- Disabling the cache is useful during schema migrations or debugging when you want every statement to be re-prepared.
- Internally thread-confined: each
connectionowns one cache, so no extra locking is necessary.
statement_cache_config
| Member | Description |
|---|---|
std::size_t capacity = 32 | Number of statements stored before older entries are evicted. |
bool enabled = true | Allows disabling caching entirely while keeping the structure. |
Example usage
sqlite::connection db{"app.db"};
db.configure_statement_cache({.capacity = 128, .enabled = true});
sqlite::execute insert{db, "INSERT INTO events(payload) VALUES(?)"};
for (auto const & payload : batch) {
insert % payload;
insert();
}
db.clear_statement_cache(); // finalize all statements before fork/exec
Most of the time query and
execute touch the cache for you—I only tweak the
config when I know a workload reuses dozens of statements.
statement_cache
| Member | Description |
|---|---|
explicit statement_cache(statement_cache_config cfg = {}) | Constructs a cache with the given configuration. |
sqlite3_stmt* acquire(sqlite3* db, std::string_view sql) | Returns a cached statement (or prepares a new one if absent). |
void release(std::string_view sql, sqlite3_stmt* stmt) | Marks a statement as idle so it can be reused later. |
void clear(sqlite3* db) | Finalizes all cached statements for db. |
statement_cache_config config() const noexcept | Returns the current configuration. |