Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

feat: add request caching service (#280) #315

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

aashish-g03
Copy link

This PR adds request caching service with different customizable options.

Closes #280.

@ethan-tbd
Copy link
Contributor

as part of this pr, could you please include test cases to make sure that the request caching service works as intended!

@aashish-g03
Copy link
Author

@ethan-tbd I’ve added test cases for the service. Will this be considered part of the same task, or will I receive extra points for it?


/// Fetches data from the cache and API.
Stream<T> fetchData(String url) async* {
final box = await Hive.openBox(networkCacheKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of opening and closing Box whenever fetchData() is called, we can just have RequestCacheService take in a Box (take a look at PfisNotifier, VcsNotifier for some examples)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to generate a hashed cache key here, we can just use url as the key and have multiple requests to the same url live in the same Box

// Else, we have already emitted cached data, so we can silently fail or log the error
}
} finally {
await box.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this if RequestCacheService is updated to take in a Box

Comment on lines 45 to 53
if (now.difference(cachedTimestamp) < cacheDuration) {
// Cache is still valid, but we'll fetch new data to check for updates
// return; // Uncomment this line to skip fetching new data
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can just be a parameter in fetchData() that defaults to fetching new data

import 'package:didpay/shared/services/request_cache_service.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:hive_test/hive_test.dart';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's stay away from adding new packages just for testing! once RequestCacheService is updated to take in a Box, you can update this test file to make use of MockBox in mocks.dart (take a look at the test files for PfisNotifier and VcsNotifier and try to mimic the structure of how we have been testing those notifiers)

@ethan-tbd
Copy link
Contributor

Will this be considered part of the same task, or will I receive extra points for it?

I created a subissue #318, make sure to be thorough with the test cases and try to follow the existing pattern for how we test Hive boxes!

@taniashiba
Copy link
Contributor

Hi @aashish-g03 - could you please resolve the above comments so that @ethan-tbd may review/close your PR? Thank you!

@taniashiba
Copy link
Contributor

Hi @aashish-g03 - If you'd like for your contribution here to count towards Hacktoberfest, please review the comments above so that @ethan-tbd may close and add the hacktoberfest-accepted label to your PR for you to earn points. If you need any help, let us know. ❤️

feat: add tests for request caching service (TBD54566975#280)

fix: fix box import and tests for request caching service (TBD54566975#280)
@aashish-g03
Copy link
Author

@taniashiba @ethan-tbd I've updated the service file according to your suggestions and included some suggestions at the end of service test file to enhance testing. Apologies for the delay—I experienced a sudden increase in office workload that limited my response time.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create request caching service
3 participants