Skip to content

Commit

Permalink
Saves the content into application folder instead database
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacfi committed Mar 10, 2021
1 parent 09ed4cc commit ddb5a63
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -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"}
{"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"}
6 changes: 3 additions & 3 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
66 changes: 61 additions & 5 deletions lib/src/store/store_disk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand Down Expand Up @@ -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)
)
''';
Expand Down Expand Up @@ -111,27 +113,37 @@ class DiskCacheStore extends ICacheStore {
Future<CacheObj> 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<bool> 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,
{
_columnKey: obj.key,
_columnSubKey: obj.subKey ?? "",
_columnMaxAgeDate: obj.maxAgeDate ?? 0,
_columnMaxStaleDate: obj.maxStaleDate ?? 0,
_columnContent: content,
_columnFileName: fileName,
_columnStatusCode: obj.statusCode,
_columnHeaders: headers
},
Expand All @@ -143,8 +155,22 @@ class DiskCacheStore extends ICacheStore {
Future<bool> 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);
}

Expand All @@ -156,17 +182,47 @@ class DiskCacheStore extends ICacheStore {

Future<bool> _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<String> _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<bool> 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);
}

Expand Down
12 changes: 7 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

1 comment on commit ddb5a63

@isaacfi
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an enhancement proposed in the issue 61 this support long content into the cache saving it into the file system.

Please sign in to comment.