dio_cache_interceptor 0.5.1

Flutter Android iOS

Dio HTTP cache interceptor with multiple stores respecting HTTP directives (ETag, Last-Modified, ...) with options.

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

HTTP directives (currently):

  • ETag
  • Last-Modified

Options #

CacheOptions is available widely on interceptor and on requests to take precedence over your global settings.

See documentation for all properties.

Stores #

  • BackupCacheStore: Combined store with primary and secondary.
  • DbCacheStore: Cache with DB (sqflite).
  • FileCacheStore: Cache with file system.
  • MemCacheStore: Volatile cache with LRU strategy.

Usage #

import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';

// The store used.
final cacheStore = DbCacheStore();

// Add cache interceptor and global options
final dio = Dio()
  ..interceptors.add(DioCacheInterceptor(
    options: const CacheOptions(
      store: cacheStore, // Required. The store used.
      policy: CachePolicy.requestFirst, // Default. Requests firt and caches response.
      hitCacheOnErrorExcept: [401, 403], // Returns a cached response on error if available but for statuses 401 & 403.
      priority: CachePriority.normal, // Default. Priority to separate cache entries.
      maxStale: const Duration(days: 7), // Optional. Override any HTTP directive to delete entry past this duration.
    )
  )
);

// Request with global options
var response = await dio.get('http://www.foo.com');

// Apply specific options for the current request
response = await dio.get('http://www.foo.com',
  options: Options(
    extra: CacheOptions(
      policy: CachePolicy.refresh,
      store: cacheStore,
    ).toExtra(),
  ),
);

Options #

Cache policy #

enum CachePolicy {
  /// Forces to return the cached value if available.
  /// Requests otherwise.
  cacheFirst,

  /// Forces to return the cached value if available.
  /// Requests otherwise.
  /// Caches response regardless directives.
  ///
  /// In short, you'll save every successful GET requests.
  cacheStoreForce,

  /// Requests and skips cache save even if
  /// response has cache directives.
  cacheStoreNo,

  /// Forces to request, even if a valid
  /// cache is available.
  refresh,

  /// Requests and caches if
  /// response has cache directives.
  requestFirst,
}

Roadmap #

  • Cache-Control (a subset)
  • Code coverage

Features and bugs #

Please file feature requests and bugs at the issue tracker.

License #

License.

1
likes
100
pub points
50%
popularity

Publisher

openapi4j.org

Dio HTTP cache interceptor with multiple stores respecting HTTP directives (ETag, Last-Modified, ...) with options.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache 2.0 (LICENSE)

Dependencies

dio, flutter, meta, path, sqflite, uuid

More

Packages that depend on dio_cache_interceptor