From ce9e4d55e77c5d3c3e117342943e6bd72102feb7 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Thu, 14 Jul 2022 07:50:27 +0200 Subject: [PATCH] harbour: Open cache database before starting app --- packages/harbour/lib/main.dart | 4 ++- .../lib/src/utils/request_manager.dart | 35 +++++++------------ 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/packages/harbour/lib/main.dart b/packages/harbour/lib/main.dart index d04ca74b..13112077 100644 --- a/packages/harbour/lib/main.dart +++ b/packages/harbour/lib/main.dart @@ -31,7 +31,9 @@ Future main() async { final sharedPreferences = await SharedPreferences.getInstance(); - final requestManager = RequestManager(platform); + final cache = Cache(platform); + await cache.init(); + final requestManager = RequestManager(cache); final globalOptions = GlobalOptions( Storage('global', sharedPreferences), diff --git a/packages/harbour/lib/src/utils/request_manager.dart b/packages/harbour/lib/src/utils/request_manager.dart index 28c355b4..945598bc 100644 --- a/packages/harbour/lib/src/utils/request_manager.dart +++ b/packages/harbour/lib/src/utils/request_manager.dart @@ -1,10 +1,9 @@ part of '../harbour.dart'; class RequestManager { - RequestManager(final HarbourPlatform platform) { - _cache = Cache(platform); - } - late final Cache _cache; + RequestManager(this._cache); + + final Cache _cache; final bool _enablePrinting = false; @@ -182,7 +181,7 @@ class Cache { final HarbourPlatform _platform; Database? _database; - Future _ensureInitialized() async { + Future init() async { if (_database != null) { return; } @@ -199,26 +198,16 @@ class Cache { ); } - Future has(final String key) async { - await _ensureInitialized(); - - return Sqflite.firstIntValue(await _database!.rawQuery('SELECT COUNT(*) FROM cache WHERE key = ?', [key])) == 1; - } - - Future get(final String key) async { - await _ensureInitialized(); + Future has(final String key) async => + Sqflite.firstIntValue(await _database!.rawQuery('SELECT COUNT(*) FROM cache WHERE key = ?', [key])) == 1; - return (await _database!.rawQuery('SELECT value FROM cache WHERE key = ?', [key]))[0]['value'] as String?; - } - - Future set(final String key, final String value) async { - await _ensureInitialized(); + Future get(final String key) async => + (await _database!.rawQuery('SELECT value FROM cache WHERE key = ?', [key]))[0]['value'] as String?; - await _database!.rawQuery( - 'INSERT INTO cache (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value', - [key, value], - ); - } + Future set(final String key, final String value) async => _database!.rawQuery( + 'INSERT INTO cache (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value', + [key, value], + ); } class ResultCached implements Result {