Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to flutter_cache_manager version 2.1.0 #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"flutterSdkVersion": "1.24.0-10.2.pre"
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions benchmark/lib/benchmark.dart
Original file line number Diff line number Diff line change
@@ -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<CacheInfoRepository> Function();
Expand All @@ -18,7 +18,7 @@ Future<BenchmarkResult> benchmark(

// perform some operation
final n = samples.length;
final ops = List<int>(n);
final ops = List<int>.filled(n, 0);

final s2 = Stopwatch()..start();
for (var i = 0; i < n; i++) {
Expand Down
11 changes: 6 additions & 5 deletions benchmark/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -117,15 +116,16 @@ class _MyHomePageState extends State<MyHomePage> {
),
Text(
'Operation Average: ${item.opsAvg.prettyTime()}'),
Text('Operation Median: ${item.opsMedian.prettyTime()}')
Text(
'Operation Median: ${item.opsMedian.prettyTime()}')
]),
),
);
}),
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.
);
}
Expand Down Expand Up @@ -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<void> cleanRepo(RepoMaker r) async {
Expand Down
12 changes: 4 additions & 8 deletions benchmark/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,22 @@ 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:
sdk: flutter

# 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:

Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
}
Expand Down
10 changes: 3 additions & 7 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
76 changes: 14 additions & 62 deletions lib/src/hive_cache_manager.dart
Original file line number Diff line number Diff line change
@@ -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> box}) {
factory HiveCacheManager({
@required Future<Box> 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<String> getFilePath() async {
var directory = await getTemporaryDirectory();
return p.join(directory.path, key);
}
}

CacheStore _buildCacheStore(int maxSize, Duration maxAge,
{@required String storeKey,
@required Future<Box> 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<f.Directory> _createFileDir(String storeKey) async {
var fs = const LocalFileSystem();
var directory = fs.directory((await _getFilePath(storeKey)));
await directory.create(recursive: true);
return directory;
}

Future<String> _getFilePath(String storeKey) async {
var directory = await getTemporaryDirectory();
return p.join(directory.path, storeKey);
}

CacheStore _createStoreForWeb(
int maxSize, Duration maxAge, String storeKey, Future<Box> 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<CacheInfoRepository> _hiveProvider(Future<Box> box) async {
final provider = HiveCacheObjectProvider(box);
await provider.open();
return provider;
HiveCacheManager._(Config config) : super(config);
}
28 changes: 22 additions & 6 deletions lib/src/hive_cache_object_provider.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,8 +13,9 @@ class HiveCacheObjectProvider implements CacheInfoRepository {
HiveCacheObjectProvider(this.box);

@override
Future open() async {
Future<bool> open() async {
_box = await box;
return true;
}

@override
Expand All @@ -23,7 +24,8 @@ class HiveCacheObjectProvider implements CacheInfoRepository {
}

@override
Future<CacheObject> insert(CacheObject cacheObject) async {
Future<CacheObject> insert(CacheObject cacheObject,
{bool setTouchedToNow = true}) async {
HiveCacheObject hiveCacheObject;
if (cacheObject is HiveCacheObject) {
hiveCacheObject = cacheObject;
Expand Down Expand Up @@ -51,12 +53,14 @@ class HiveCacheObjectProvider implements CacheInfoRepository {
}

@override
Future deleteAll(Iterable<int> ids) async {
Future<int> deleteAll(Iterable<int> ids) async {
unawaited(_box.deleteAll(ids.map((id) => id.toString()).toList()));
return ids.length;
}

@override
Future<int> update(CacheObject cacheObject) async {
Future<int> update(CacheObject cacheObject,
{bool setTouchedToNow = true}) async {
unawaited(updateOrInsert(cacheObject));
return 1;
}
Expand Down Expand Up @@ -100,12 +104,24 @@ class HiveCacheObjectProvider implements CacheInfoRepository {
}

@override
Future close() async {
Future<bool> close() async {
// this is usually never called
await _box.compact();
await _box.close();
return true;
}

@override
Future<void> deleteDataFile() async {
await _box.clear();
}

@override
Future<bool> 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();
12 changes: 6 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ environment:
sdk: ">=2.6.0 <3.0.0"

dependencies:
clock: ^1.0.1
file: ^5.1.0
flutter:
sdk: flutter
flutter_cache_manager: ^1.4.1
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: "^1.6.4"
path_provider: "^1.4.0"
pedantic: "^1.8.0+1"
clock: ^1.0.1
file: ^5.1.0
rxdart: '>=0.23.1 <0.25.0'
meta: ^1.1.8
uuid: "^2.0.2"