invokeCache method
It reads the cache from the storage, checks if it's valid, and returns the response if it is
Args: key (String): The key to store the cache data. storage (Storage): The storage to use. keepExpiredCache (bool): If true, the cache will be returned even if it has expired. Defaults to false ttlValue (Duration): The time to live value for the cache. Defaults to defaultTTLValue
Returns: A Future<Response?>
Implementation
Future<Response?> invokeCache({
required String key,
required Storage storage,
bool keepExpiredCache = false,
Duration ttlValue = defaultTTLValue,
}) async {
final value = await storage.read(key);
if (value != null) {
final cacheWrapper = CacheWrapper.fromJson(jsonDecode(value));
if (_isValid(cacheWrapper, keepExpiredCache, ttlValue)) {
if (kDebugMode) {
print("Fetch cache data for key $key: ${cacheWrapper.response}");
}
return cacheWrapper.response;
}
}
return null;
}