Skip to content

Commit ca5ac62

Browse files
committed
feat: add request caching service (TBD54566975#280)
1 parent 0bc1feb commit ca5ac62

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
4+
import 'package:collection/collection.dart';
5+
import 'package:hive/hive.dart';
6+
import 'package:http/http.dart' as http;
7+
8+
class RequestCacheService<T> {
9+
final T Function(Map<String, dynamic>) fromJson;
10+
final Map<String, dynamic> Function(T) toJson;
11+
final Duration cacheDuration;
12+
final String networkCacheKey;
13+
14+
RequestCacheService({
15+
required this.fromJson,
16+
required this.toJson,
17+
this.cacheDuration = const Duration(minutes: 1),
18+
this.networkCacheKey = 'network_cache',
19+
});
20+
21+
/// Fetches data from the cache and API.
22+
Stream<T> fetchData(String url) async* {
23+
final box = await Hive.openBox(networkCacheKey);
24+
final cacheKey = _generateCacheKey(url);
25+
bool cacheEmitted = false;
26+
27+
try {
28+
// Check for cached data
29+
final cachedEntry = await box.get(cacheKey);
30+
if (cachedEntry != null && cachedEntry is Map) {
31+
final cachedMap = Map<String, dynamic>.from(cachedEntry);
32+
final cachedTimestamp =
33+
DateTime.parse(cachedMap['timestamp'] as String);
34+
final cachedDataJson =
35+
Map<String, dynamic>.from(cachedMap['data'] ?? {});
36+
final cachedData = fromJson(cachedDataJson);
37+
// Emit cached data
38+
yield cachedData;
39+
cacheEmitted = true;
40+
41+
final now = DateTime.now();
42+
// Decide whether to fetch new data based on cache validity
43+
if (now.difference(cachedTimestamp) < cacheDuration) {
44+
// Cache is still valid, but we'll fetch new data to check for updates
45+
// return; // Uncomment this line to skip fetching new data
46+
}
47+
}
48+
49+
// Fetch data from the network
50+
try {
51+
final response = await http.get(Uri.parse(url));
52+
53+
if (response.statusCode == 200) {
54+
final dataMap = jsonDecode(response.body) as Map<String, dynamic>;
55+
final data = fromJson(dataMap);
56+
57+
// Compare with cached data
58+
bool isDataUpdated = true;
59+
if (cachedEntry != null && cachedEntry is Map) {
60+
final cachedDataJson =
61+
Map<String, dynamic>.from(cachedEntry['data'] ?? {});
62+
// Use DeepCollectionEquality for deep comparison
63+
const equalityChecker = DeepCollectionEquality();
64+
if (equalityChecker.equals(cachedDataJson, dataMap)) {
65+
isDataUpdated = false;
66+
}
67+
}
68+
69+
// Cache the new data with a timestamp
70+
final cacheEntry = {
71+
'data': toJson(data),
72+
'timestamp': DateTime.now().toIso8601String(),
73+
};
74+
await box.put(cacheKey, cacheEntry);
75+
76+
if (isDataUpdated) {
77+
yield data;
78+
}
79+
} else {
80+
throw Exception(
81+
'Failed to load data from $url. Status code: ${response.statusCode}',
82+
);
83+
}
84+
} catch (e) {
85+
if (!cacheEmitted) {
86+
// No cached data was emitted before, so we need to throw an error
87+
throw Exception('Error fetching data from $url: $e');
88+
}
89+
// Else, we have already emitted cached data, so we can silently fail or log the error
90+
}
91+
} finally {
92+
await box.close();
93+
}
94+
}
95+
96+
/// Generates a cache key by hashing the URL.
97+
String _generateCacheKey(String url) {
98+
return url.hashCode.toString();
99+
}
100+
}

0 commit comments

Comments
 (0)