diff --git a/packages/sqlite_async/CHANGELOG.md b/packages/sqlite_async/CHANGELOG.md index 5d18052..d690ea1 100644 --- a/packages/sqlite_async/CHANGELOG.md +++ b/packages/sqlite_async/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.14.4 + +- Add `SqliteOptions.preparedStatementCacheSize` to cache prepared statements. + ## 0.14.3 - Include identifier of mutexes when a navigator lock attempt is aborted. diff --git a/packages/sqlite_async/lib/src/native/database/native_sqlite_database.dart b/packages/sqlite_async/lib/src/native/database/native_sqlite_database.dart index 8e6651d..5e73abc 100644 --- a/packages/sqlite_async/lib/src/native/database/native_sqlite_database.dart +++ b/packages/sqlite_async/lib/src/native/database/native_sqlite_database.dart @@ -270,7 +270,8 @@ final class NativeSqliteDatabaseImpl extends SqliteDatabaseImpl { [ for (var i = 0; i < maxReaders; i++) openConnection(isWriter: false) ], - // TODO: Option to enable prepared statement cache. + preparedStatementCacheSize: + openFactory.sqliteOptions.preparedStatementCacheSize, ); }, ); diff --git a/packages/sqlite_async/lib/src/sqlite_options.dart b/packages/sqlite_async/lib/src/sqlite_options.dart index d722f4e..be20dee 100644 --- a/packages/sqlite_async/lib/src/sqlite_options.dart +++ b/packages/sqlite_async/lib/src/sqlite_options.dart @@ -43,6 +43,16 @@ final class SqliteOptions { /// additional readers on the web. final int maxReaders; + /// The amount of prepared statements to cache. + /// + /// For each SQLite connection, up to [preparedStatementCacheSize] statements + /// will be cached in an LRU cache. This allows re-using prepared statements + /// instead of parsing and optimizing them again, which improve performance + /// for frequently-used statements like watched queries. + /// + /// Defaults to 16. To disable caching prepared statements, set this to 0. + final int preparedStatementCacheSize; + @Deprecated('Use default SqliteOptions constructor instead') const factory SqliteOptions.defaults() = SqliteOptions; @@ -54,6 +64,7 @@ final class SqliteOptions { this.lockTimeout = const Duration(seconds: 30), this.profileQueries = _profileQueriesByDefault, this.maxReaders = defaultMaxReaders, + this.preparedStatementCacheSize = 16, }); /// Creates a new options instance by applying overrides from parameters. @@ -64,6 +75,7 @@ final class SqliteOptions { WebSqliteOptions? webSqliteOptions, bool? profileQueries, int? maxReaders, + int? preparedStatementCacheSize, }) { return SqliteOptions( journalMode: journalMode, @@ -73,6 +85,8 @@ final class SqliteOptions { lockTimeout: lockTimeout, profileQueries: profileQueries ?? this.profileQueries, maxReaders: maxReaders ?? this.maxReaders, + preparedStatementCacheSize: + preparedStatementCacheSize ?? this.preparedStatementCacheSize, ); } diff --git a/packages/sqlite_async/lib/src/web/web_sqlite_open_factory.dart b/packages/sqlite_async/lib/src/web/web_sqlite_open_factory.dart index bcd6592..eaf875c 100644 --- a/packages/sqlite_async/lib/src/web/web_sqlite_open_factory.dart +++ b/packages/sqlite_async/lib/src/web/web_sqlite_open_factory.dart @@ -64,7 +64,8 @@ base class WebSqliteOpenFactory extends InternalOpenFactory { /// can customize the behavior where needed. Future connectToWorker( WebSqlite sqlite, String name) { - return sqlite.connectToRecommended(name); + return sqlite.connectToRecommended(name, + preparedStatementCacheSize: sqliteOptions.preparedStatementCacheSize); } /// Currently this only uses the SQLite Web WASM implementation. diff --git a/packages/sqlite_async/pubspec.yaml b/packages/sqlite_async/pubspec.yaml index 2975811..1a2f88e 100644 --- a/packages/sqlite_async/pubspec.yaml +++ b/packages/sqlite_async/pubspec.yaml @@ -14,8 +14,8 @@ topics: dependencies: sqlite3: ^3.2.0 - sqlite3_web: '>=0.8.0 <0.10.0' - sqlite3_connection_pool: ^0.2.4 + sqlite3_web: ^0.9.2 + sqlite3_connection_pool: ^0.2.7 async: ^2.10.0 collection: ^1.17.0 meta: ^1.10.0