dio-http-cache

Pub

中文介绍

Dio-http-cache is a cache library for Dio ( http client for flutter ), like Rxcache in Android.

Dio-http-cache uses sqflite as disk cache, and LRU strategy as memory cache.

Inspired by flutter_cache_manager.

Add Dependency

dependencies:
  dio_http_cache: ^0.2.x #latest version

QuickStart

  1. Add a dio-http-cache interceptor in Dio :

    dio.interceptors.add(DioCacheManager(CacheConfig(baseUrl: "http://www.google.com")).interceptor);
    
  2. Set maxAge for a request :

    Dio().get(
      "http://www.google.com",
      options: buildCacheOptions(Duration(days: 7)),
    );
    

The advanced

  1. Custom your config by buildCacheOptions :

    1. primaryKey: By default, host + path is used as the primaryKey, and you can also customize it.

    2. subKey: By default, query ( data or queryParameters) is used as subKey, and you can specify the subKey when it's necessary, for example:

      buildCacheOptions(Duration(days: 7), subKey: "page=1")
      
    3. maxAge: set the cache time. If the value is null or not setted, it will try to get maxAge and maxStale from response headers.

    4. maxStale: set stale time. When an error (like 500,404) occurs before maxStale, try to return cache.

      buildCacheOptions(Duration(days: 7), maxStale: Duration(days: 10))
      
    5. forceRefresh: false default.

      buildCacheOptions(Duration(days: 7), forceRefresh: true)
      
      1. Get data from network first.
      2. If getting data from network succeeds, store or refresh cache.
      3. If getting data from network fails or no network avaliable, try get data from cache instead of an error.
  2. Use "CacheConfig" to config default params

    1. encrypt / decrypt: These two must be used together to encrypt the disk cache data, you can also zip data here.
    2. DefaultMaxAge: use Duration(day:7) as default.
    3. DefaultaMaxStale: similar with DefaultMaxAge.
    4. DatabaseName: database name.
    5. SkipMemoryCache: false defalut.
    6. SkipDiskCache: false default.
    7. MaxMemoryCacheCount: 100 defalut.
  3. How to clear expired cache

    • Just ignore it, that is automatic.

    • But if you insist : DioCacheManager.clearExpired();

  4. How to delete caches

    1. No matter what subKey is, delete local cache if primary matched.

      // Automatically parses primarykey from path
      _dioCacheManager.deleteByPrimaryKey(path); 
      
    2. Delete local cache when both primaryKey and subKey matched.

      // delete local cache when both primaryKey and subKey matched.
      _dioCacheManager.deleteByPrimaryKeyAndSubKey(path); 
      

      INPORTANT: If you have additional parameters when requesting the http interface, you must take them with it, for example:

      _dio.get(_url, queryParameters: {'k': keyword}, 
        options: buildCacheOptions(Duration(hours: 1)))
      //delete the cache:
      _dioCacheManager.deleteByPrimaryKeyAndSubKey(_url, queryParameters:{'k': keyword}); 
      
      _dio.post(_url, data: {'k': keyword}, 
        options: buildCacheOptions(Duration(hours: 1)))
      //delete the cache:
      _dioCacheManager.deleteByPrimaryKeyAndSubKey(_url, data:{'k': keyword}); 
      
    3. Delete local cache by primaryKey and optional subKey if you know your primarykey and subkey exactly.

      // delete local cache by primaryKey and optional subKey
      _dioCacheManager.delete(primaryKey,{subKey});
      
  5. How to clear All caches (expired or not)

    _dioCacheManager.clearAll();
    

Example for maxAge and maxStale

_dio.post(
	"https://www.exmaple.com",
	data: {'k': "keyword"},
	options:buildCacheOptions(
  		Duration(days: 3), 
  		maxStale: Duration(days: 7), 
	)
)
  1. 0 ~ 3 days : Return data from cache directly (irrelevant with network).
  2. 3 ~ 7 days:
    1. Get data from network first.
    2. If getting data from network succeeds, refresh cache.
    3. If getting data from network fails or no network avaliable, try get data from cache instead of an error.
  3. 7 ~ ∞ days: It won't use cache anymore, and the cache will be deleted at the right time.

License

Copyright 2019 Hurshi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Libraries

dio_http_cache

Dart

VM

dart:ffi
Foreign Function Interface for interoperability with the C programming language. [...]

Web

dart:html
HTML elements and other resources for web-based applications that need to interact with the browser and the DOM (Document Object Model). [...]
dart:js
Low-level support for interoperating with JavaScript. [...]
dart:js_util
Utility methods to efficiently manipulate typed JSInterop objects in cases where the name to call is not known at runtime. You should only use these methods when the same effect cannot be achieved with @JS annotations. These methods would be extension methods on JSObject if Dart supported extension methods.