Giter Club home page Giter Club logo

dio_cache_interceptor's Introduction

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

HTTP directives:

  • ETag
  • Last-Modified
  • Cache-Control
  • Date
  • Expires

Stores

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

Usage

import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';

// Global options
final options = const CacheOptions(
  store: DbCacheStore(), // Required.
  policy: CachePolicy.requestFirst, // Default. Requests first and caches response.
  hitCacheOnErrorExcept: [401, 403], // Optional. Returns a cached response on error if available but for statuses 401 & 403.
  priority: CachePriority.normal, // Optional. Default. Allows 3 cache levels and ease cleanup.
  maxStale: const Duration(days: 7), // Very optional. Overrides any HTTP directive to delete entry past this duration.
);

// Add cache interceptor with global/default options
final dio = Dio()
  ..interceptors.add(DioCacheInterceptor(options: options),
);

...

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

// Request with dedicated options
response = await dio.get('http://www.foo.com',
  options: Options(
    extra: CacheOptions(policy: CachePolicy.refresh).toExtra(),
  ),
);

Options

CacheOptions are widely available on interceptor and on requests to take precedence.
There is no merge behaviour between interceptor and dedicated request options but store property.

See documentation for all properties.

Encryption

Optionally, you can encrypt content and headers with your own algorithm via Encrypt / Decrypt methods if your storage is located in public folder.

Cache policy

enum CachePolicy {
  /// Returns 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.
  /// 
  /// Note: previously stored response stays untouched.
  cacheStoreNo,

  /// Forces to request, even if a valid
  /// cache is available and caches if
  /// response has cache directives.
  refresh,

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

Features and bugs

Please file feature requests and bugs at the issue tracker.

Testing

From example folder:

flutter run test/test_suite.dart

License

License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.