dio_cache 0.1.0-beta dio_cache: ^0.1.0-beta copied to clipboard
A plugin for dio that caches responses for better optimization and offline data access.
dio_cache #
A plugin for dio that caches responses for better optimization and offline data access.
Usage #
import 'package:dio_cache/dio_cache.dart';
Basic configuration
final dio = Dio()
..interceptors.add(CacheInterceptor());
Global caching options
final dio = Dio()
..interceptors.add(CacheInterceptor(
options: const CacheInterceptorRequestExtra(
forceUpdate: false, // Forces to update even if cache isn't expired
forceCache: false, // Forces to use cache, even if expired
priority: CachePriority.normal, // Setting a priority to clean only several requests
returnCacheOnError: true, // Returns a cached response on error if available
isCached: true, // Bypass caching if [false]
expiry: const Duration(minutes: 1), // The cache expiry, after which a new request is triggered instead of getting the cached response
)
)
);
Defining the caching store
final dio = Dio()
..interceptors.add(CacheInterceptor(
store: FileCacheStore(Directory('.cache')),
)
);
Sending a request with options
final forcedResponse = await dio.get("http://www.flutter.dev", options: Options(
extra: CacheInterceptorRequestExtra(
forceUpdate: true
).toExtra(),
));
Invalidating a cached value
interceptor.store.invalidate("GET", "http://www.flutter.dev");
Cleaning cached values
interceptor.store.clean(CachePriority.low);
Features and bugs #
Please file issues.