dio_http_cache 0.1.2 dio_http_cache: ^0.1.2 copied to clipboard
http cache lib for Flutter dio like RxCache.It use sqflite as disk cache,and google/quiver-dart/LRU strategy as memory cache.
dio-http-cache #
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 #
dio_http_cache: ^0.1.2
QuickStart #
-
Add a dio-http-cache interceptor in Dio :
dio.interceptors.add(DioCacheManager(CacheConfig()).interceptor);
-
Set maxAge for a request :
Dio().get( "http://www.google.com", options: buildCacheOptions(Duration(days: 7)), );
The advanced #
-
Custom your config by buildCacheOptions :
-
MaxAge: the only required param, set the cache time;
-
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))
-
subKey: dio-http-cache uses url as key,you can add a subKey when it's necessary, such as one request with different params.
buildCacheOptions(Duration(days: 7), subKey: "page=1")
-
forceRefresh: false default.
buildCacheOptions(Duration(days: 7), forceRefresh: true)
- Get data from network first.
- If getting data from network succeeds, store or refresh cache.
- If getting data from network fails or no network avaliable, try get data from cache instead of an error.
-
-
Use "CacheConfig" to config default params
- encrypt / decrypt: These two must be used together to encrypt the disk cache data, otherwise use base64 as default.
- DefaultMaxAge: use
Duration(day:7)
as default. - DefaultaMaxStale: similar with DefaultMaxAge.
- DatabaseName: database name.
- SkipMemoryCache: false defalut.
- SkipDiskCache: false default.
- MaxMemoryCacheCount: 100 defalut.
-
How to clear expired cache
-
Just ignore it, that is automatic.
-
But if you insist :
DioCacheManager.clearExpired();
-
-
How to delete one cache
_dioCacheManager.delete(url); //delete all the cache with url as the key _dioCacheManager.delete(url,subKey);
-
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),
)
)
- 0 ~ 3 days : Return data from cache directly (irrelevant with network).
- 3 ~ 7 days:
- Get data from network first.
- If getting data from network succeeds, refresh cache.
- If getting data from network fails or no network avaliable, try get data from cache instead of an error.
- 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.