Skip to content

Commit

Permalink
fix: Get parity with refresh/request and refreshForceCache/forceCache.
Browse files Browse the repository at this point in the history
  • Loading branch information
llfbandit committed Apr 16, 2021
1 parent 2a4f3b6 commit af9d769
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 10 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 2.1.1
- fix: refreshForceCache policy added.
Get parity with refresh/request and refreshForceCache/forceCache.

## 2.1.0
feat: Add Hive as cache store.
- feat: Add Hive as cache store.

## 2.0.0
- core: Update dio to 4.0.0.
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,20 @@ enum CachePolicy {
/// Forces to return the cached value if available.
/// Requests otherwise.
/// Caches response regardless directives.
forceCache,
/// Requests regardless cache availability.
/// Caches response regardless directives.
///
/// In short, you'll save every successful GET requests.
forceCache,
refreshForceCache,
/// Requests and skips cache save even if
/// response has cache directives.
noCache,
/// Forces to request, even if a valid
/// cache is available and caches if
/// response has cache directives.
/// Requests regardless cache availability.
/// Caches if response has cache directives.
refresh,
/// Returns the cached value if available (and un-expired).
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class _MyAppState extends State<MyApp> {
),
RaisedButton(
onPressed: () async => await _noCacheCall(),
child: Text('Call (Cache no store policy)'),
child: Text('Call (No cache policy)'),
),
Text(text),
],
Expand Down
6 changes: 4 additions & 2 deletions lib/src/dio_cache_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class DioCacheInterceptor extends Interceptor {

final options = _getCacheOptions(request);

if (options.policy != CachePolicy.refresh) {
if (options.policy != CachePolicy.refresh &&
options.policy != CachePolicy.refreshForceCache) {
final cacheResp = await _getCacheResponse(request);
if (cacheResp != null) {
if (_shouldReturnCache(options, cacheResp)) {
Expand Down Expand Up @@ -142,7 +143,8 @@ class DioCacheInterceptor extends Interceptor {
}

bool _hasCacheDirectives(Response response, {CachePolicy? policy}) {
if (policy == CachePolicy.forceCache) {
if (policy == CachePolicy.forceCache ||
policy == CachePolicy.refreshForceCache) {
return true;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/src/model/cache_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ enum CachePolicy {
/// Forces to return the cached value if available.
/// Requests otherwise.
/// Caches response regardless directives.
forceCache,

/// Requests regardless cache availability.
/// Caches response regardless directives.
///
/// In short, you'll save every successful GET requests.
forceCache,
refreshForceCache,

/// Requests and skips cache save even if
/// response has cache directives.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Dio HTTP cache interceptor with multiple stores respecting HTTP dir
repository: https://github.com/llfbandit/dio_cache_interceptor
issue_tracker: https://github.com/llfbandit/dio_cache_interceptor/issues

version: 2.1.0
version: 2.1.1

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
25 changes: 25 additions & 0 deletions test/cache_interceptor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,31 @@ void main() {
expect(resp.extra[CacheResponse.cacheKey], isNull);
});

test('Fetch force policy', () async {
// 1st time fetch
var resp = await _dio.get(
'${MockHttpClientAdapter.mockBase}/ok-nodirective',
options: options.copyWith(policy: CachePolicy.forceCache).toOptions(),
);
expect(resp.statusCode, equals(200));
expect(resp.extra[CacheResponse.fromNetwork], isTrue);
// 2nd time cache
resp = await _dio.get(
'${MockHttpClientAdapter.mockBase}/ok-nodirective',
options: options.copyWith(policy: CachePolicy.forceCache).toOptions(),
);
expect(resp.statusCode, equals(304));
expect(resp.extra[CacheResponse.fromNetwork], isFalse);
// 3rd time fetch
resp = await _dio.get(
'${MockHttpClientAdapter.mockBase}/ok-nodirective',
options:
options.copyWith(policy: CachePolicy.refreshForceCache).toOptions(),
);
expect(resp.statusCode, equals(200));
expect(resp.extra[CacheResponse.fromNetwork], isTrue);
});

test('Fetch refresh policy', () async {
final resp = await _dio.get('${MockHttpClientAdapter.mockBase}/ok');
final cacheKey = resp.extra[CacheResponse.cacheKey];
Expand Down
8 changes: 8 additions & 0 deletions test/mock_httpclient_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ class MockHttpClientAdapter extends HttpClientAdapter {
'last-modified': ['Wed, 21 Oct 2045 07:28:00 GMT'],
},
);
case '/ok-nodirective':
return ResponseBody.fromString(
jsonEncode({'path': uri.path}),
200,
headers: {
Headers.contentTypeHeader: [Headers.jsonContentType],
},
);
case '/post':
return ResponseBody.fromString(
jsonEncode({'path': uri.path}),
Expand Down

0 comments on commit af9d769

Please sign in to comment.