pub package codecov

HTTP cache for the http package, following HTTP directives (or not) with multiple stores.

Also available as a dio interceptor.

HTTP directives

  • Cache-Control (max-age, max-stale, min-fresh, must-revalidate, no-cache, no-store),
  • ETag, Last-Modified, Expires, Date and Age.

Stores

  • BackupCacheStore: Combined store with primary and secondary.
  • DriftCacheStore: Cache with Drift Get it.
  • FileCacheStore: Cache with file system (Does nothing on web platform) Get it.
  • HiveCacheStore: Cache using hive_ce package Get it.
  • IsarCacheStore: Cache using Isar package (available on all platforms) Get it.
  • ObjectBoxCacheStore: Cache using ObjectBox package (no web support) Get it.
  • SembastCacheStore: Cache using Sembast package Get it.
  • MMKVCacheStore: Cache using MMKV package (iOS & Android only) Get it.
  • MemCacheStore: Volatile cache with LRU strategy.

Usage

As client

import 'package:http/http.dart' as http;
import 'package:http_cache_client/http_cache_client.dart';

final options = CacheOptions(store: MemCacheStore());

final client = CacheClient(
  http.Client(),
  options: options,
  // Optional: per-request options when none are given, null keeps the client ones.
  optionsFor: (request) => request.url.path.startsWith('/live')
      ? options.copyWith(policy: CachePolicy.noCache)
      : null,
);

// 200: stored. Next calls are served from the cache, or revalidated (304).
var response = await client.get(Uri.parse('https://www.foo.com'));

// Per-request options.
response = await client.get(
  Uri.parse('https://www.foo.com'),
  options: options.copyWith(policy: CachePolicy.refresh),
);

As client owner

To own the send (timeouts, cancellation, retries) and still fall back to the stored copy on error, use HttpCache steps:

final cache = HttpCache(options);

Future<http.StreamedResponse> send(http.BaseRequest request) async {
  final lookup = await cache.lookup(request);
  if (lookup.response case final cached?) return cached;

  try {
    // lookup.request may be a copy with validators, yours is never modified.
    var response = await cache.onResponse(
      lookup,
      await inner.send(lookup.request).timeout(timeout),
    );
    // 304 but the cached entry is gone: send the request again.
    if (cache.refetch(lookup, response) case final retry?) {
      await response.stream.drain<void>();
      response = await cache.onResponse(
        retry,
        await inner.send(retry.request).timeout(timeout),
      );
    }
    // Read the body here too to fall back on body errors.
    return response;
  } catch (e, st) {
    return await cache.onError(lookup, e) ?? Error.throwWithStackTrace(e, st);
  }
}

Handling cache with client only

Follow those instructions if needed.

Options

CacheOptions is set on the client, and can be overridden per request.

See the API documentation for all properties.

Features and bugs

Please file feature requests and bugs at the issue tracker.

License

License.

Libraries

http_cache_client