Skip to content

Commit ddb5a63

Browse files
committed
Saves the content into application folder instead database
1 parent 09ed4cc commit ddb5a63

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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"}
1+
{"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"}

example/ios/Flutter/flutter_export_environment.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/bin/sh
22
# This is a generated file; do not edit or check into version control.
3-
export "FLUTTER_ROOT=/Users/silvertree/Documents/dev/flutter"
4-
export "FLUTTER_APPLICATION_PATH=/Users/silvertree/Documents/codes/dio-http-cache/example"
3+
export "FLUTTER_ROOT=/usr/local/share/flutter"
4+
export "FLUTTER_APPLICATION_PATH=/Users/isaacfi/Code/dio-http-cache/example"
55
export "FLUTTER_TARGET=lib/main.dart"
66
export "FLUTTER_BUILD_DIR=build"
77
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
88
export "OTHER_LDFLAGS=$(inherited) -framework Flutter"
9-
export "FLUTTER_FRAMEWORK_DIR=/Users/silvertree/Documents/dev/flutter/bin/cache/artifacts/engine/ios"
9+
export "FLUTTER_FRAMEWORK_DIR=/usr/local/share/flutter/bin/cache/artifacts/engine/ios"
1010
export "FLUTTER_BUILD_NAME=1.0.0"
1111
export "FLUTTER_BUILD_NUMBER=1"
1212
export "DART_OBFUSCATION=false"

lib/src/store/store_disk.dart

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import 'package:dio_http_cache/src/core/config.dart';
44
import 'package:dio_http_cache/src/core/obj.dart';
55
import 'package:dio_http_cache/src/store/store_impl.dart';
66
import 'package:path/path.dart';
7+
import 'package:path_provider/path_provider.dart';
78
import 'package:sqflite/sqflite.dart';
9+
import 'package:uuid/uuid.dart';
810

911
class DiskCacheStore extends ICacheStore {
1012
final String _databasePath;
@@ -16,7 +18,7 @@ class DiskCacheStore extends ICacheStore {
1618
final String _columnSubKey = "subKey";
1719
final String _columnMaxAgeDate = "max_age_date";
1820
final String _columnMaxStaleDate = "max_stale_date";
19-
final String _columnContent = "content";
21+
final String _columnFileName = "file_name";
2022
final String _columnStatusCode = "statusCode";
2123
final String _columnHeaders = "headers";
2224

@@ -59,9 +61,9 @@ class DiskCacheStore extends ICacheStore {
5961
$_columnSubKey text,
6062
$_columnMaxAgeDate integer,
6163
$_columnMaxStaleDate integer,
62-
$_columnContent BLOB,
64+
$_columnFileName text,
6365
$_columnStatusCode integer,
64-
$_columnHeaders BLOB,
66+
$_columnHeaders blob,
6567
PRIMARY KEY ($_columnKey, $_columnSubKey)
6668
)
6769
''';
@@ -111,27 +113,37 @@ class DiskCacheStore extends ICacheStore {
111113
Future<CacheObj> getCacheObj(String key, {String subKey}) async {
112114
var db = await _database;
113115
if (null == db) return null;
116+
final cachePath = await _createCacheDir();
114117
var where = "$_columnKey=\"$key\"";
115118
if (null != subKey) where += " and $_columnSubKey=\"$subKey\"";
116119
var resultList = await db.query(_tableCacheObject, where: where);
117120
if (null == resultList || resultList.length <= 0) return null;
118-
return await _decryptCacheObj(CacheObj.fromJson(resultList[0]));
121+
var cacheObj = CacheObj.fromJson(resultList[0]);
122+
var cacheFilePath = join(cachePath, resultList[0]['file_name']);
123+
final file = File(cacheFilePath);
124+
cacheObj.content = file.readAsBytesSync();
125+
return await _decryptCacheObj(cacheObj);
119126
}
120127

121128
@override
122129
Future<bool> setCacheObj(CacheObj obj) async {
123130
var db = await _database;
124131
if (null == db) return false;
132+
final cacheDirPath = await _createCacheDir();
133+
var fileName = 'cache_' + Uuid().v4().replaceAll('-', '');
134+
final cacheFilePath = join(cacheDirPath, fileName);
135+
final file = File(cacheFilePath);
125136
var content = await _encryptCacheStr(obj.content);
126137
var headers = await _encryptCacheStr(obj.headers);
138+
await file.writeAsBytes(content);
127139
await db.insert(
128140
_tableCacheObject,
129141
{
130142
_columnKey: obj.key,
131143
_columnSubKey: obj.subKey ?? "",
132144
_columnMaxAgeDate: obj.maxAgeDate ?? 0,
133145
_columnMaxStaleDate: obj.maxStaleDate ?? 0,
134-
_columnContent: content,
146+
_columnFileName: fileName,
135147
_columnStatusCode: obj.statusCode,
136148
_columnHeaders: headers
137149
},
@@ -143,8 +155,22 @@ class DiskCacheStore extends ICacheStore {
143155
Future<bool> delete(String key, {String subKey}) async {
144156
var db = await _database;
145157
if (null == db) return false;
158+
final cacheDirPath = await _createCacheDir();
159+
final cacheDir = Directory(cacheDirPath);
160+
if (!cacheDir.existsSync()) {
161+
cacheDir.createSync(recursive: true);
162+
}
146163
var where = "$_columnKey=\"$key\"";
147164
if (null != subKey) where += " and $_columnSubKey=\"$subKey\"";
165+
var resultList = await db.query(_tableCacheObject, where: where);
166+
if (null == resultList || resultList.length <= 0) return false;
167+
resultList.forEach((ri) {
168+
final cacheFilePath = join(cacheDirPath, ri['file_name']);
169+
final file = File(cacheFilePath);
170+
if (file.existsSync()) {
171+
file.deleteSync();
172+
}
173+
});
148174
return 0 != await db.delete(_tableCacheObject, where: where);
149175
}
150176

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

157183
Future<bool> _clearExpired(Database db) async {
158184
if (null == db) return false;
185+
final cacheDirPath = await _createCacheDir();
186+
final cacheDir = Directory(cacheDirPath);
187+
if (!cacheDir.existsSync()) {
188+
cacheDir.createSync(recursive: true);
189+
}
159190
var now = DateTime.now().millisecondsSinceEpoch;
160191
var where1 = "$_columnMaxStaleDate > 0 and $_columnMaxStaleDate < $now";
161192
var where2 = "$_columnMaxStaleDate <= 0 and $_columnMaxAgeDate < $now";
193+
var resultList =
194+
await db.query(_tableCacheObject, where: "( $where1 ) or ( $where2 )");
195+
if (null == resultList || resultList.length <= 0) return false;
196+
resultList.forEach((ri) {
197+
final cacheFilePath = join(cacheDirPath, ri['file_name']);
198+
final file = File(cacheFilePath);
199+
if (file.existsSync()) {
200+
file.deleteSync();
201+
}
202+
});
162203
return 0 !=
163204
await db.delete(_tableCacheObject, where: "( $where1 ) or ( $where2 )");
164205
}
165206

207+
Future<String> _createCacheDir() async {
208+
final cachePath =
209+
join((await getApplicationDocumentsDirectory()).path, _databaseName);
210+
final cacheDir = Directory(cachePath);
211+
if (!(await cacheDir.exists())) {
212+
await cacheDir.create(recursive: true);
213+
}
214+
return cachePath;
215+
}
216+
166217
@override
167218
Future<bool> clearAll() async {
168219
var db = await _database;
169220
if (null == db) return false;
221+
final cacheDirPath = await _createCacheDir();
222+
final cacheDir = Directory(cacheDirPath);
223+
if (await cacheDir.exists()) {
224+
await cacheDir.delete(recursive: true);
225+
}
170226
return 0 != await db.delete(_tableCacheObject);
171227
}
172228

pubspec.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ environment:
1111
dependencies:
1212
flutter:
1313
sdk: flutter
14-
quiver: ^2.0.3
15-
json_serializable: ^3.0.0
16-
json_annotation: ^3.0.0
14+
crypto: ^2.1.1+1
1715
dio: ^3.0.1
16+
json_annotation: ^3.0.0
17+
json_serializable: ^3.0.0
18+
path: ^1.7.0
19+
path_provider: ^1.6.18
20+
quiver: ^2.0.3
1821
sqflite: ^1.1.6+3
19-
path: ^1.6.2
20-
crypto: ^2.1.1+1
22+
uuid: ^2.2.2
2123

2224
dev_dependencies:
2325
flutter_test:

0 commit comments

Comments
 (0)