|
Lightweight 0.20260921.0
|
A bounded LRU pool of already-prepared ODBC statement handles, owned by a SqlConnection.
More...
#include <SqlPreparedStatementCache.hpp>
Classes | |
| struct | PreparedHandle |
| A pooled statement handle together with the parameter count the driver reported for it. More... | |
| struct | Statistics |
| Cumulative counters, primarily for tests and diagnostics. More... | |
Public Member Functions | |
| LIGHTWEIGHT_API | SqlPreparedStatementCache (std::size_t capacity=0) noexcept |
| Constructs a cache with the given capacity. | |
| LIGHTWEIGHT_API | ~SqlPreparedStatementCache () noexcept |
| Frees every pooled statement handle. | |
| SqlPreparedStatementCache (SqlPreparedStatementCache const &)=delete | |
| SqlPreparedStatementCache & | operator= (SqlPreparedStatementCache const &)=delete |
| SqlPreparedStatementCache (SqlPreparedStatementCache &&)=delete | |
| SqlPreparedStatementCache & | operator= (SqlPreparedStatementCache &&)=delete |
| std::size_t | Capacity () const noexcept |
| LIGHTWEIGHT_API void | SetCapacity (std::size_t capacity) noexcept |
| Sets the capacity, evicting the least recently released handles when shrinking. | |
| bool | IsEnabled () const noexcept |
| std::size_t | Size () const noexcept |
| Statistics const & | Stats () const noexcept |
| LIGHTWEIGHT_API void | ResetStatistics () noexcept |
| Resets the cumulative counters to zero, leaving the pooled handles untouched. | |
| void | RecordDirectReuse () noexcept |
| LIGHTWEIGHT_API std::optional< PreparedHandle > | Acquire (std::string_view query) noexcept |
Takes an idle handle prepared for query out of the pool. | |
| LIGHTWEIGHT_API void | Release (std::string_view query, PreparedHandle handle) noexcept |
| Hands a prepared handle back to the pool as the most recently used entry. | |
| LIGHTWEIGHT_API void | Clear () noexcept |
| Frees every pooled handle, e.g. after DDL invalidated the cached query plans. | |
A bounded LRU pool of already-prepared ODBC statement handles, owned by a SqlConnection.
Preparing a statement costs a server-side parse on MS SQL Server and PostgreSQL. Neither driver pays it inside SQLPrepare, which sends nothing: it rides along with the first execute of the freshly prepared handle, and a matching deallocate follows when the handle is freed. This cache keeps the SQLHSTMT handles of recently prepared queries alive, so a repeat of the same SQL text on the same connection re-executes a prepared handle instead. Measured behind a 50 ms link, that is worth about three round-trips per query on psqlODBC and one on the Microsoft driver.
A handle is checked out while a statement uses it: Acquire removes it from the pool and Release puts it back. Two statements preparing the same query at the same time therefore each get their own handle, and both are pooled afterwards (subject to the capacity bound). Eviction is least-recently-released first, which matters because several backends cap the number of live prepared statements per session.
SqlConnection: one connection is used by one thread at a time. SqlConnection::ClearPreparedStatementCache — Lightweight's own migration paths do it for you. Definition at line 59 of file SqlPreparedStatementCache.hpp.
|
explicitnoexcept |
Constructs a cache with the given capacity.
| capacity | Maximum number of idle prepared handles to keep; 0 disables the cache. |
|
inlinenoexcept |
0 when disabled). Definition at line 103 of file SqlPreparedStatementCache.hpp.
|
noexcept |
Sets the capacity, evicting the least recently released handles when shrinking.
| capacity | Maximum number of idle prepared handles to keep; 0 disables and clears. |
|
inlinenoexcept |
Definition at line 113 of file SqlPreparedStatementCache.hpp.
|
inlinenoexcept |
Definition at line 119 of file SqlPreparedStatementCache.hpp.
|
inlinenoexcept |
Definition at line 125 of file SqlPreparedStatementCache.hpp.
|
inlinenoexcept |
Counts a prepare that reused the statement's own handle instead of the pool.
Definition at line 135 of file SqlPreparedStatementCache.hpp.
References Lightweight::SqlPreparedStatementCache::Statistics::directReuses.
|
noexcept |
Takes an idle handle prepared for query out of the pool.
The caller owns the returned handle until it hands it back via Release (or frees it).
| query | The exact SQL text the handle must have been prepared with. |
std::nullopt when no idle handle matches.
|
noexcept |
Hands a prepared handle back to the pool as the most recently used entry.
The caller must have closed the handle's cursor and unbound its columns beforehand. Ownership of handle transfers to the cache; when the capacity bound is exceeded — or the cache is disabled — the surplus handle is freed right away.
| query | The SQL text handle is prepared for. |
| handle | The prepared handle to pool. |