http_cache_client 1.0.2 copy "http_cache_client: ^1.0.2" to clipboard
http_cache_client: ^1.0.2 copied to clipboard

HTTP cache client with multiple stores respecting HTTP directives (or not).

pub package codecov

HTTP cache client with multiple stores respecting HTTP directives (or not).

Also available as interceptor for dio package.

HTTP directives: #

Cache triggers ETag
Last-Modified
Date
Cache freshness Age
Date
Expires
max-age (Cache-Control)
max-stale (Cache-Control)
min-fresh (Cache-Control)
must-revalidate
Request date (added by interceptor)
Response date (added by interceptor)
Cache commutators no-cache (Cache-Control)
no-store (Cache-Control request & response)

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.
  • MemCacheStore: Volatile cache with LRU strategy.

Usage #

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

// Global options
final options = const CacheOptions(
  // A default store is required for the client.
  store: MemCacheStore(),

  // All subsequent fields are optional to get a standard behaviour.
  
  // Default.
  policy: CachePolicy.request,
  // Returns a previous cached response on error for given status codes.
  // Defaults to `[]`.
  hitCacheOnErrorCodes: [500],
  // Allows to return a cached response on network errors (e.g. offline usage).
  // Defaults to `false`.
  hitCacheOnNetworkFailure: true,
  // Overrides any HTTP directive to delete entry past this duration.
  // Useful only when origin server has no cache config or custom behaviour is desired.
  // Defaults to `null`.
  maxStale: const Duration(days: 7),
  // Default. Allows 3 cache sets and ease cleanup.
  priority: CachePriority.normal,
  // Default. Body and headers encryption with your own algorithm.
  cipher: null,
  // Default. Key builder to retrieve requests.
  keyBuilder: CacheOptions.defaultCacheKeyBuilder,
  // Default. Allows to cache POST requests.
  // Assigning a [keyBuilder] is strongly recommended when `true`.
  allowPostMethod: false,
);

// Add cache interceptor with global/default options
final client = CacheClient(http.Client(), options: options);

// ...

// Requesting with global options => status(200) => Content is written to cache store.
var response = await client.get('https://www.foo.com');
// Requesting with global options => status(304 => 200) => Content is read from cache store.
response = await client.get('https://www.foo.com');

// Requesting by modifying policy with refresh option
// for this single request => status(200) => Content is written to cache store
response = await client.get('https://www.foo.com',
  options: options.copyWith(policy: CachePolicy.refresh),
);
copied to clipboard

Handling cache with client only #

Follow those intructions if needed.

Options #

CacheOptions is widely available on client and on requests to take precedence.

See documentation for all properties.

Encryption #

Optionally, you can encrypt body and headers with your own algorithm via CacheCipher.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

License #

License.

3
likes
160
points
261
downloads

Publisher

verified publisheropenapi4j.org

Weekly Downloads

2024.09.22 - 2025.04.06

HTTP cache client with multiple stores respecting HTTP directives (or not).

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

http, http_cache_core

More

Packages that depend on http_cache_client