diff --git a/example/.flutter-plugins-dependencies b/example/.flutter-plugins-dependencies index d9e1def..177cf16 100644 --- a/example/.flutter-plugins-dependencies +++ b/example/.flutter-plugins-dependencies @@ -1 +1 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"sqflite","path":"/Users/silvertree/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+1/","dependencies":[]}],"android":[{"name":"sqflite","path":"/Users/silvertree/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+1/","dependencies":[]}],"macos":[{"name":"sqflite","path":"/Users/silvertree/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+1/","dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"sqflite","dependencies":[]}],"date_created":"2020-09-24 18:44:13.977860","version":"1.20.3"} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"path_provider","path":"/usr/local/share/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.27/","dependencies":[]},{"name":"sqflite","path":"/usr/local/share/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.2+4/","dependencies":[]}],"android":[{"name":"path_provider","path":"/usr/local/share/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.27/","dependencies":[]},{"name":"sqflite","path":"/usr/local/share/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.2+4/","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"/usr/local/share/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-0.0.4+8/","dependencies":[]},{"name":"sqflite","path":"/usr/local/share/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.2+4/","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/usr/local/share/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-0.0.1+2/","dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/usr/local/share/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_windows-0.0.4+3/","dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux","path_provider_windows"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"sqflite","dependencies":[]}],"date_created":"2021-03-10 08:36:28.914758","version":"1.22.5"} \ No newline at end of file diff --git a/example/ios/Flutter/flutter_export_environment.sh b/example/ios/Flutter/flutter_export_environment.sh index 3fd2a96..add7df4 100755 --- a/example/ios/Flutter/flutter_export_environment.sh +++ b/example/ios/Flutter/flutter_export_environment.sh @@ -1,12 +1,12 @@ #!/bin/sh # This is a generated file; do not edit or check into version control. -export "FLUTTER_ROOT=/Users/silvertree/Documents/dev/flutter" -export "FLUTTER_APPLICATION_PATH=/Users/silvertree/Documents/codes/dio-http-cache/example" +export "FLUTTER_ROOT=/usr/local/share/flutter" +export "FLUTTER_APPLICATION_PATH=/Users/isaacfi/Code/dio-http-cache/example" export "FLUTTER_TARGET=lib/main.dart" export "FLUTTER_BUILD_DIR=build" export "SYMROOT=${SOURCE_ROOT}/../build/ios" export "OTHER_LDFLAGS=$(inherited) -framework Flutter" -export "FLUTTER_FRAMEWORK_DIR=/Users/silvertree/Documents/dev/flutter/bin/cache/artifacts/engine/ios" +export "FLUTTER_FRAMEWORK_DIR=/usr/local/share/flutter/bin/cache/artifacts/engine/ios" export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NUMBER=1" export "DART_OBFUSCATION=false" diff --git a/lib/src/store/store_disk.dart b/lib/src/store/store_disk.dart index d81ea53..4366b14 100644 --- a/lib/src/store/store_disk.dart +++ b/lib/src/store/store_disk.dart @@ -4,7 +4,9 @@ import 'package:dio_http_cache/src/core/config.dart'; import 'package:dio_http_cache/src/core/obj.dart'; import 'package:dio_http_cache/src/store/store_impl.dart'; import 'package:path/path.dart'; +import 'package:path_provider/path_provider.dart'; import 'package:sqflite/sqflite.dart'; +import 'package:uuid/uuid.dart'; class DiskCacheStore extends ICacheStore { final String _databasePath; @@ -16,7 +18,7 @@ class DiskCacheStore extends ICacheStore { final String _columnSubKey = "subKey"; final String _columnMaxAgeDate = "max_age_date"; final String _columnMaxStaleDate = "max_stale_date"; - final String _columnContent = "content"; + final String _columnFileName = "file_name"; final String _columnStatusCode = "statusCode"; final String _columnHeaders = "headers"; @@ -59,9 +61,9 @@ class DiskCacheStore extends ICacheStore { $_columnSubKey text, $_columnMaxAgeDate integer, $_columnMaxStaleDate integer, - $_columnContent BLOB, + $_columnFileName text, $_columnStatusCode integer, - $_columnHeaders BLOB, + $_columnHeaders blob, PRIMARY KEY ($_columnKey, $_columnSubKey) ) '''; @@ -111,19 +113,29 @@ class DiskCacheStore extends ICacheStore { Future getCacheObj(String key, {String subKey}) async { var db = await _database; if (null == db) return null; + final cachePath = await _createCacheDir(); var where = "$_columnKey=\"$key\""; if (null != subKey) where += " and $_columnSubKey=\"$subKey\""; var resultList = await db.query(_tableCacheObject, where: where); if (null == resultList || resultList.length <= 0) return null; - return await _decryptCacheObj(CacheObj.fromJson(resultList[0])); + var cacheObj = CacheObj.fromJson(resultList[0]); + var cacheFilePath = join(cachePath, resultList[0]['file_name']); + final file = File(cacheFilePath); + cacheObj.content = file.readAsBytesSync(); + return await _decryptCacheObj(cacheObj); } @override Future setCacheObj(CacheObj obj) async { var db = await _database; if (null == db) return false; + final cacheDirPath = await _createCacheDir(); + var fileName = 'cache_' + Uuid().v4().replaceAll('-', ''); + final cacheFilePath = join(cacheDirPath, fileName); + final file = File(cacheFilePath); var content = await _encryptCacheStr(obj.content); var headers = await _encryptCacheStr(obj.headers); + await file.writeAsBytes(content); await db.insert( _tableCacheObject, { @@ -131,7 +143,7 @@ class DiskCacheStore extends ICacheStore { _columnSubKey: obj.subKey ?? "", _columnMaxAgeDate: obj.maxAgeDate ?? 0, _columnMaxStaleDate: obj.maxStaleDate ?? 0, - _columnContent: content, + _columnFileName: fileName, _columnStatusCode: obj.statusCode, _columnHeaders: headers }, @@ -143,8 +155,22 @@ class DiskCacheStore extends ICacheStore { Future delete(String key, {String subKey}) async { var db = await _database; if (null == db) return false; + final cacheDirPath = await _createCacheDir(); + final cacheDir = Directory(cacheDirPath); + if (!cacheDir.existsSync()) { + cacheDir.createSync(recursive: true); + } var where = "$_columnKey=\"$key\""; if (null != subKey) where += " and $_columnSubKey=\"$subKey\""; + var resultList = await db.query(_tableCacheObject, where: where); + if (null == resultList || resultList.length <= 0) return false; + resultList.forEach((ri) { + final cacheFilePath = join(cacheDirPath, ri['file_name']); + final file = File(cacheFilePath); + if (file.existsSync()) { + file.deleteSync(); + } + }); return 0 != await db.delete(_tableCacheObject, where: where); } @@ -156,17 +182,47 @@ class DiskCacheStore extends ICacheStore { Future _clearExpired(Database db) async { if (null == db) return false; + final cacheDirPath = await _createCacheDir(); + final cacheDir = Directory(cacheDirPath); + if (!cacheDir.existsSync()) { + cacheDir.createSync(recursive: true); + } var now = DateTime.now().millisecondsSinceEpoch; var where1 = "$_columnMaxStaleDate > 0 and $_columnMaxStaleDate < $now"; var where2 = "$_columnMaxStaleDate <= 0 and $_columnMaxAgeDate < $now"; + var resultList = + await db.query(_tableCacheObject, where: "( $where1 ) or ( $where2 )"); + if (null == resultList || resultList.length <= 0) return false; + resultList.forEach((ri) { + final cacheFilePath = join(cacheDirPath, ri['file_name']); + final file = File(cacheFilePath); + if (file.existsSync()) { + file.deleteSync(); + } + }); return 0 != await db.delete(_tableCacheObject, where: "( $where1 ) or ( $where2 )"); } + Future _createCacheDir() async { + final cachePath = + join((await getApplicationDocumentsDirectory()).path, _databaseName); + final cacheDir = Directory(cachePath); + if (!(await cacheDir.exists())) { + await cacheDir.create(recursive: true); + } + return cachePath; + } + @override Future clearAll() async { var db = await _database; if (null == db) return false; + final cacheDirPath = await _createCacheDir(); + final cacheDir = Directory(cacheDirPath); + if (await cacheDir.exists()) { + await cacheDir.delete(recursive: true); + } return 0 != await db.delete(_tableCacheObject); } diff --git a/pubspec.yaml b/pubspec.yaml index ce92ccf..5a0e1dc 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,13 +11,15 @@ environment: dependencies: flutter: sdk: flutter - quiver: ^2.0.3 - json_serializable: ^3.0.0 - json_annotation: ^3.0.0 + crypto: ^2.1.1+1 dio: ^3.0.1 + json_annotation: ^3.0.0 + json_serializable: ^3.0.0 + path: ^1.7.0 + path_provider: ^1.6.18 + quiver: ^2.0.3 sqflite: ^1.1.6+3 - path: ^1.6.2 - crypto: ^2.1.1+1 + uuid: ^2.2.2 dev_dependencies: flutter_test: