Skip to content

Commit

Permalink
fix: fix box import and tests for request caching service (TBD5456697…
Browse files Browse the repository at this point in the history
  • Loading branch information
aashish-g03 committed Nov 1, 2024
1 parent 1bb9ebd commit a664391
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 61 deletions.
31 changes: 18 additions & 13 deletions lib/shared/services/request_cache_service.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:developer';

import 'package:collection/collection.dart';
import 'package:hive/hive.dart';
Expand All @@ -11,24 +12,27 @@ class RequestCacheService<T> {
final Duration cacheDuration;
final String networkCacheKey;
final http.Client httpClient;
final bool checkForUpdates;
final Box cacheBox;

RequestCacheService({
required this.fromJson,
required this.toJson,
required this.cacheBox,
this.cacheDuration = const Duration(minutes: 1),
this.networkCacheKey = 'network_cache',
http.Client? httpClient,
this.checkForUpdates = false,
}) : httpClient = httpClient ?? http.Client();

/// Fetches data from the cache and API.
Stream<T> fetchData(String url) async* {
final box = await Hive.openBox(networkCacheKey);
final cacheKey = _generateCacheKey(url);
final cacheKey = url;
bool cacheEmitted = false;

try {
// Check for cached data
final cachedEntry = await box.get(cacheKey);
final cachedEntry = await cacheBox.get(cacheKey);
if (cachedEntry != null && cachedEntry is Map) {
final cachedMap = Map<String, dynamic>.from(cachedEntry);
final cachedTimestamp =
Expand All @@ -42,9 +46,10 @@ class RequestCacheService<T> {

final now = DateTime.now();
// Decide whether to fetch new data based on cache validity
if (now.difference(cachedTimestamp) < cacheDuration) {
if (now.difference(cachedTimestamp) < cacheDuration &&
!checkForUpdates) {
// Cache is still valid, but we'll fetch new data to check for updates
// return; // Uncomment this line to skip fetching new data
return;
}
}

Expand Down Expand Up @@ -73,7 +78,7 @@ class RequestCacheService<T> {
'data': toJson(data),
'timestamp': DateTime.now().toIso8601String(),
};
await box.put(cacheKey, cacheEntry);
await cacheBox.put(cacheKey, cacheEntry);

if (isDataUpdated) {
yield data;
Expand All @@ -90,13 +95,13 @@ class RequestCacheService<T> {
}
// Else, we have already emitted cached data, so we can silently fail or log the error
}
} finally {
await box.close();
} catch (e) {
if (!cacheEmitted) {
// No cached data was emitted before, so we need to throw an error
throw Exception('Error fetching data from $url: $e');
}
// Else, we have already emitted cached data, so we can silently fail or log the error
log('Error fetching data from $url: $e');
}
}

/// Generates a cache key by hashing the URL.
String _generateCacheKey(String url) {
return url.hashCode.toString();
}
}
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ dependencies:
url_launcher: ^6.2.5
webview_flutter: ^4.4.2
web5: ^0.4.0
hive_test: ^1.0.1

dev_dependencies:
flutter_test:
Expand Down
Loading

0 comments on commit a664391

Please sign in to comment.