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

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

Open
aashish-g03 wants to merge 1 commit into
TBD54566975:mainfrom
aashish-g03:main
Open

feat: add request caching service (#280)#315
aashish-g03 wants to merge 1 commit into
TBD54566975:mainfrom
aashish-g03:main

Conversation

@aashish-g03

Copy link
Copy Markdown

This PR adds request caching service with different customizable options.

Closes #280.

@ethanwlee

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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)

@ethanwlee

Copy link
Copy Markdown
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!

@taniacryptid

Copy link
Copy Markdown
Contributor

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

@taniacryptid

Copy link
Copy Markdown
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
Copy Markdown
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