From 37aefceb74338ca5094934c31f2cb77b07fc16ef Mon Sep 17 00:00:00 2001 From: x-wei Date: Sun, 3 Jan 2021 14:35:07 +0100 Subject: [PATCH 1/3] $ fvm use 1.24.0-10.2.pre --- .fvm/fvm_config.json | 3 +++ .gitignore | 1 + 2 files changed, 4 insertions(+) create mode 100644 .fvm/fvm_config.json diff --git a/.fvm/fvm_config.json b/.fvm/fvm_config.json new file mode 100644 index 0000000..7dede6a --- /dev/null +++ b/.fvm/fvm_config.json @@ -0,0 +1,3 @@ +{ + "flutterSdkVersion": "1.24.0-10.2.pre" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 09941d4..526a3c3 100644 --- a/.gitignore +++ b/.gitignore @@ -93,3 +93,4 @@ flutter_export_environment.sh !**/ios/**/default.perspectivev3 !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages example/.flutter-plugins-dependencies +.fvm/flutter_sdk From d555cb28de84cd38056392120c6ab782661b1aec Mon Sep 17 00:00:00 2001 From: x-wei Date: Sun, 3 Jan 2021 14:41:17 +0100 Subject: [PATCH 2/3] sort and upgrade flutter_cache_manager --- pubspec.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index bc152af..41542fa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,14 +9,14 @@ environment: dependencies: flutter: sdk: flutter - flutter_cache_manager: ^1.4.1 + clock: ^1.0.1 + file: ^5.1.0 + flutter_cache_manager: ^2.1.0 hive: ^1.4.1+1 - path_provider: "^1.4.0" - uuid: "^2.0.2" http: "^0.12.0+2" + meta: ^1.1.8 + path_provider: "^1.4.0" path: "^1.6.4" pedantic: "^1.8.0+1" - clock: ^1.0.1 - file: ^5.1.0 rxdart: '>=0.23.1 <0.25.0' - meta: ^1.1.8 \ No newline at end of file + uuid: "^2.0.2" \ No newline at end of file From 718e5ffd2602be1ef899bf591aa3a71dfeb7b5a8 Mon Sep 17 00:00:00 2001 From: x-wei Date: Sun, 3 Jan 2021 15:15:29 +0100 Subject: [PATCH 3/3] upgrade implementation of HiveCacheObjectProvider --- benchmark/lib/benchmark.dart | 4 +- benchmark/lib/main.dart | 11 ++-- benchmark/pubspec.yaml | 12 ++-- example/lib/main.dart | 2 +- example/pubspec.yaml | 10 +--- lib/src/hive_cache_manager.dart | 76 +++++-------------------- lib/src/hive_cache_object_provider.dart | 28 +++++++-- pubspec.yaml | 6 +- 8 files changed, 55 insertions(+), 94 deletions(-) diff --git a/benchmark/lib/benchmark.dart b/benchmark/lib/benchmark.dart index ed4ba95..5d675d5 100644 --- a/benchmark/lib/benchmark.dart +++ b/benchmark/lib/benchmark.dart @@ -1,4 +1,4 @@ -import 'package:flutter_cache_manager/src/storage/cache_info_repository.dart'; +import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_cache_manager/src/storage/cache_object.dart'; typedef RepoMaker = Future Function(); @@ -18,7 +18,7 @@ Future benchmark( // perform some operation final n = samples.length; - final ops = List(n); + final ops = List.filled(n, 0); final s2 = Stopwatch()..start(); for (var i = 0; i < n; i++) { diff --git a/benchmark/lib/main.dart b/benchmark/lib/main.dart index 28d88f8..f6eb65a 100644 --- a/benchmark/lib/main.dart +++ b/benchmark/lib/main.dart @@ -6,7 +6,6 @@ import 'package:flutter/material.dart'; import 'package:path/path.dart' as p; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; -import 'package:flutter_cache_manager/src/storage/cache_object_provider.dart'; import 'package:flutter_cache_manager/src/storage/cache_object.dart'; import 'package:flutter_cache_manager_hive/flutter_cache_manager_hive.dart'; import 'package:flutter_cache_manager_hive/src/hive_cache_object_provider.dart'; @@ -117,7 +116,8 @@ class _MyHomePageState extends State { ), Text( 'Operation Average: ${item.opsAvg.prettyTime()}'), - Text('Operation Median: ${item.opsMedian.prettyTime()}') + Text( + 'Operation Median: ${item.opsMedian.prettyTime()}') ]), ), ); @@ -125,7 +125,7 @@ class _MyHomePageState extends State { floatingActionButton: FloatingActionButton( onPressed: _runBenchmark, tooltip: 'Measure', - child: Icon(Icons.timer), + child: const Icon(Icons.timer), ), // This trailing comma makes auto-formatting nicer for build methods. ); } @@ -156,11 +156,12 @@ final RepoMaker sqflite = () async { await Directory(databasesPath).create(recursive: true); } catch (_) {} final path = p.join(databasesPath, 'image-cache.db'); - return CacheObjectProvider(path); + return CacheObjectProvider(path: path); }; final RepoMaker hive = () async { - return HiveCacheObjectProvider(Hive.openBox('image-caching-box')); + final repo = HiveCacheObjectProvider(Hive.openBox('image-caching-box')); + return Future.value(repo); }; Future cleanRepo(RepoMaker r) async { diff --git a/benchmark/pubspec.yaml b/benchmark/pubspec.yaml index 487fe89..ebcfa07 100644 --- a/benchmark/pubspec.yaml +++ b/benchmark/pubspec.yaml @@ -9,18 +9,15 @@ environment: sdk: ">=2.7.0 <3.0.0" dependencies: + cupertino_icons: ^0.1.3 flutter: sdk: flutter + flutter_cache_manager: ^2.1.0 + flutter_cache_manager_hive: + path: ../ hive: ^1.4.1+1 hive_flutter: ^0.3.0+2 sqflite: ^1.3.1 - flutter_cache_manager_hive: - path: ../ - - - # The following adds the Cupertino Icons font to your application. - # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^0.1.3 dev_dependencies: flutter_test: @@ -28,7 +25,6 @@ dev_dependencies: # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec - # The following section is specific to Flutter. flutter: diff --git a/example/lib/main.dart b/example/lib/main.dart index faacaad..dd6a038 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -129,7 +129,7 @@ class Fab extends StatelessWidget { return FloatingActionButton( onPressed: downloadFile, tooltip: 'Download', - child: Icon(Icons.cloud_download), + child: const Icon(Icons.cloud_download), ); } } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 0015157..0f7af61 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -21,17 +21,13 @@ environment: sdk: ">=2.7.0 <3.0.0" dependencies: + cupertino_icons: ^0.1.3 flutter: sdk: flutter - hive: ^1.4.1+1 - hive_flutter: ^0.3.0+2 flutter_cache_manager_hive: path: ../ - - - # The following adds the Cupertino Icons font to your application. - # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^0.1.3 + hive: ^1.4.1+1 + hive_flutter: ^0.3.0+2 dev_dependencies: flutter_test: diff --git a/lib/src/hive_cache_manager.dart b/lib/src/hive_cache_manager.dart index 18b0f15..0d1e72f 100644 --- a/lib/src/hive_cache_manager.dart +++ b/lib/src/hive_cache_manager.dart @@ -1,78 +1,30 @@ -import 'package:file/file.dart' as f; -import 'package:file/local.dart'; -import 'package:file/memory.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; -import 'package:flutter_cache_manager/src/cache_store.dart'; -import 'package:flutter_cache_manager/src/storage/cache_info_repository.dart'; import 'package:hive/hive.dart'; import 'package:meta/meta.dart'; -import 'package:path/path.dart' as p; -import 'package:path_provider/path_provider.dart'; import 'hive_cache_object_provider.dart'; -class HiveCacheManager extends BaseCacheManager { +class HiveCacheManager extends CacheManager { static const key = 'libCachedImageDataHive'; static HiveCacheManager _instance; - factory HiveCacheManager( - {int maxSize = 200, - Duration maxAge = const Duration(days: 30), - @required Future box}) { + factory HiveCacheManager({ + @required Future box, + int maxSize = 200, + Duration maxAge = const Duration(days: 30), + }) { _instance ??= HiveCacheManager._( - _buildCacheStore(maxSize, maxAge, box: box, storeKey: key)); + Config( + key, + stalePeriod: maxAge, + maxNrOfCacheObjects: maxSize, + repo: HiveCacheObjectProvider(box), + ), + ); return _instance; } - HiveCacheManager._(CacheStore cacheStore) - : super(key, cacheStore: cacheStore); - - @override - Future getFilePath() async { - var directory = await getTemporaryDirectory(); - return p.join(directory.path, key); - } -} - -CacheStore _buildCacheStore(int maxSize, Duration maxAge, - {@required String storeKey, - @required Future box, - Duration cleanupRunMinInterval = const Duration(seconds: 10)}) { - if (kIsWeb) { - return _createStoreForWeb(maxSize, maxAge, storeKey, box, - cleanupRunMinInterval: cleanupRunMinInterval); - } - return CacheStore(_createFileDir(storeKey), storeKey, maxSize, maxAge, - cleanupRunMinInterval: cleanupRunMinInterval); -} - -Future _createFileDir(String storeKey) async { - var fs = const LocalFileSystem(); - var directory = fs.directory((await _getFilePath(storeKey))); - await directory.create(recursive: true); - return directory; -} - -Future _getFilePath(String storeKey) async { - var directory = await getTemporaryDirectory(); - return p.join(directory.path, storeKey); -} - -CacheStore _createStoreForWeb( - int maxSize, Duration maxAge, String storeKey, Future box, - {Duration cleanupRunMinInterval = const Duration(seconds: 10)}) { - if (!kIsWeb) return null; - var memDir = MemoryFileSystem().systemTempDirectory.createTemp('cache'); - - return CacheStore(memDir, storeKey, maxSize, maxAge, - cacheRepoProvider: _hiveProvider(box), - cleanupRunMinInterval: cleanupRunMinInterval); -} - -Future _hiveProvider(Future box) async { - final provider = HiveCacheObjectProvider(box); - await provider.open(); - return provider; + HiveCacheManager._(Config config) : super(config); } diff --git a/lib/src/hive_cache_object_provider.dart b/lib/src/hive_cache_object_provider.dart index 9c66468..09ae21f 100644 --- a/lib/src/hive_cache_object_provider.dart +++ b/lib/src/hive_cache_object_provider.dart @@ -1,5 +1,5 @@ import 'package:clock/clock.dart'; -import 'package:flutter_cache_manager/src/storage/cache_info_repository.dart'; +import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_cache_manager/src/storage/cache_object.dart'; import 'package:hive/hive.dart'; import 'package:pedantic/pedantic.dart'; @@ -13,8 +13,9 @@ class HiveCacheObjectProvider implements CacheInfoRepository { HiveCacheObjectProvider(this.box); @override - Future open() async { + Future open() async { _box = await box; + return true; } @override @@ -23,7 +24,8 @@ class HiveCacheObjectProvider implements CacheInfoRepository { } @override - Future insert(CacheObject cacheObject) async { + Future insert(CacheObject cacheObject, + {bool setTouchedToNow = true}) async { HiveCacheObject hiveCacheObject; if (cacheObject is HiveCacheObject) { hiveCacheObject = cacheObject; @@ -51,12 +53,14 @@ class HiveCacheObjectProvider implements CacheInfoRepository { } @override - Future deleteAll(Iterable ids) async { + Future deleteAll(Iterable ids) async { unawaited(_box.deleteAll(ids.map((id) => id.toString()).toList())); + return ids.length; } @override - Future update(CacheObject cacheObject) async { + Future update(CacheObject cacheObject, + {bool setTouchedToNow = true}) async { unawaited(updateOrInsert(cacheObject)); return 1; } @@ -100,12 +104,24 @@ class HiveCacheObjectProvider implements CacheInfoRepository { } @override - Future close() async { + Future close() async { // this is usually never called await _box.compact(); await _box.close(); + return true; + } + + @override + Future deleteDataFile() async { + await _box.clear(); + } + + @override + Future exists() async { + return _box.isOpen; } } /// All keys have to be ASCII Strings with a max length of 255 chars or unsigned 32 bit integers +/// TODO: hashCode can change over dart versions, use MD5 instead?? String _hiveKey(String key) => key.hashCode.toString(); diff --git a/pubspec.yaml b/pubspec.yaml index 41542fa..20964ef 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,16 +7,16 @@ environment: sdk: ">=2.6.0 <3.0.0" dependencies: - flutter: - sdk: flutter clock: ^1.0.1 file: ^5.1.0 + flutter: + sdk: flutter flutter_cache_manager: ^2.1.0 hive: ^1.4.1+1 http: "^0.12.0+2" meta: ^1.1.8 - path_provider: "^1.4.0" path: "^1.6.4" + path_provider: "^1.4.0" pedantic: "^1.8.0+1" rxdart: '>=0.23.1 <0.25.0' uuid: "^2.0.2" \ No newline at end of file