Skip to content

Commit

Permalink
feat: Improved deleteFromPath and getFromPath methods to retrieve…
Browse files Browse the repository at this point in the history
… multiple entries.
  • Loading branch information
llfbandit committed Jan 10, 2025
1 parent 5f3b705 commit 7ed3a99
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
5 changes: 3 additions & 2 deletions dio_cache_interceptor_db_store/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## 6.0.0
- feat: Updated dependencies for make this package compatible with WASM.
- feat: Improved `deleteFromPath` and `getFromPath` methods to retrieve multiple entries.
- fix: `exists` method was not returning result from filtered key.
- chore: Raised dart SDK minimum to 3.0.0.
- chore: Raised Drift minimum to 2.9.0.
- chore: Raised Dart SDK to ^3.0.0.
- chore: Raised Drift to ^2.9.0.

## 5.1.1
- chore: Updated dependencies.
Expand Down
13 changes: 13 additions & 0 deletions dio_cache_interceptor_db_store/lib/src/store/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,17 @@ class DioCacheDao extends DatabaseAccessor<DioCacheDatabase>
url: data.url,
);
}

Future<void> deleteKeys(List<String> keys) async {
final query = delete(dioCache)..where((t) => t.cacheKey.isIn(keys));
await query.go();
}

Future<List<CacheResponse>> getMany(List<String> keys) {
final query = select(dioCache)
..where((t) => t.cacheKey.isIn(keys))
..orderBy([(t) => OrderingTerm(expression: t.date)]);

return query.get().then((e) => e.map(mapDataToResponse).toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';
import 'package:dio_cache_interceptor_db_store/src/store/database.dart';
import 'package:drift/drift.dart';

/// A store saving responses in a dedicated database
/// from an optional [directory].
Expand Down Expand Up @@ -37,7 +38,7 @@ class DbCacheStore extends CacheStore {
this.databaseName = tableName,
this.logStatements = false,
this.webSqlite3WasmPath = 'sqlite3.wasm',
this.webDriftWorkerPath = 'drift_worker.dart.js',
this.webDriftWorkerPath = 'drift_worker.js',
}) : _db = DioCacheDatabase(openDb(
databasePath: databasePath,
databaseName: databaseName,
Expand Down Expand Up @@ -68,11 +69,11 @@ class DbCacheStore extends CacheStore {
Future<void> deleteFromPath(
RegExp pathPattern, {
Map<String, String?>? queryParams,
}) async {
}) {
return _getFromPath(
pathPattern,
queryParams: queryParams,
onResponseMatch: (r) => delete(r.cacheKey),
onResult: _db.dioCacheDao.deleteKeys,
);
}

Expand All @@ -96,8 +97,8 @@ class DbCacheStore extends CacheStore {
await _getFromPath(
pathPattern,
queryParams: queryParams,
onResponseMatch: (r) async => responses.add(
_db.dioCacheDao.mapDataToResponse(r),
onResult: (keys) async => responses.addAll(
await _db.dioCacheDao.getMany(keys),
),
);

Expand All @@ -117,24 +118,31 @@ class DbCacheStore extends CacheStore {
Future<void> _getFromPath(
RegExp pathPattern, {
Map<String, String?>? queryParams,
required Future<void> Function(DioCacheData) onResponseMatch,
required Future<void> Function(List<String>) onResult,
}) async {
var results = <DioCacheData>[];
const limit = 10;
int? offset;

do {
final query = _db.dioCacheDao.select(_db.dioCacheDao.dioCache)
..limit(limit, offset: offset);
results = await query.get();

for (final result in results) {
if (pathExists(result.url, pathPattern, queryParams: queryParams)) {
await onResponseMatch(result);
}
}

offset = (offset ?? 0) + limit;
} while (results.isNotEmpty);
final cache = _db.dioCacheDao.dioCache;

final matchesPath = cache.url.regexp(
pathPattern.pattern,
multiLine: pathPattern.isMultiLine,
caseSensitive: pathPattern.isCaseSensitive,
unicode: pathPattern.isUnicode,
dotAll: pathPattern.isDotAll,
);

final results = await (_db.dioCacheDao.selectOnly(cache)
..where(matchesPath)
..addColumns([cache.cacheKey, cache.url]))
.map((result) => MapEntry(
result.read(cache.cacheKey)!,
result.read(cache.url)!,
))
.get();

results.removeWhere(
(e) => !pathExists(e.value, pathPattern, queryParams: queryParams),
);

await onResult(results.map((e) => e.key).toList());
}
}

0 comments on commit 7ed3a99

Please sign in to comment.