Skip to content

marvinluo1/dio_cache_interceptor

 
 

Repository files navigation

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

  • 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

  • Backup store (primary, secondary)
  • Cache-Control (a subset)

Features and bugs

Please file feature requests and bugs at the issue tracker.

License

License.

About

Dio HTTP cache respecting directives (or not)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Dart 91.2%
  • Ruby 7.4%
  • Other 1.4%