- Notifications
You must be signed in to change notification settings - Fork0
http cache lib for Flutter dio like RxCache
License
AdventOfVim/dio-http-cache
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Dio-http-cache is a cache library forDio ( http client for flutter ), likeRxcache in Android.
Dio-http-cache usessqflite as disk cache, andLRU strategy as memory cache.
Inspired byflutter_cache_manager.
dependencies:dio_http_cache:^0.3.x#latest version
Add a dio-http-cache interceptor in Dio :
dio.interceptors.add(DioCacheManager(CacheConfig(baseUrl:"http://www.google.com")).interceptor);
Set maxAge for a request :
Dio().get("http://www.google.com", options:buildCacheOptions(Duration(days:7)),);
Custom your config by buildCacheOptions :
primaryKey: By default,
host + pathis used as the primaryKey, and you can also customize it.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")
maxAge: set the cache time. If the value is null or not setted, it will try to get maxAge and maxStale from response headers.
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))
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
- baseUrl: it’s optional; If you don't have set baseUrl in CacheConfig, when you call
deleteCache, you need provide full path like"https://www.google.com/search?q=hello", but not just"search?q=hello". - encrypt / decrypt: these two must be used together to encrypt the disk cache data, you can also zip data here.
- defaultMaxAge: use
Duration(day:7)as default. - defaultaMaxStale: similar with DefaultMaxAge.
- databasePath: database path.
- databaseName: database name.
- skipMemoryCache: false defalut.
- skipDiskCache: false default.
- maxMemoryCacheCount: 100 defalut.
- defaultRequestMethod: use "POST" as default, it will be used in
delete caches. - diskStore: custom disk storage.
- baseUrl: it’s optional; If you don't have set baseUrl in CacheConfig, when you call
How to clear expired cache
Just ignore it, that is automatic.
But if you insist :
DioCacheManager.clearExpired();
How to delete caches
No matter what subKey is, delete local cache if primary matched.
// Automatically parses primarykey from path_dioCacheManager.deleteByPrimaryKey(path, requestMethod:"POST");
Delete local cache when both primaryKey and subKey matched.
// delete local cache when both primaryKey and subKey matched._dioCacheManager.deleteByPrimaryKeyAndSubKey(path, requestMethod:"GET");
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, requestMethod:"GET", queryParameters:{'k': keyword});
_dio.post(_url, data: {'k': keyword}, options:buildCacheOptions(Duration(hours:1)))//delete the cache:_dioCacheManager.deleteByPrimaryKeyAndSubKey(_url, requestMethod:"POST", data:{'k': keyword});
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,requestMethod});
How to clear All caches (expired or not)
_dioCacheManager.clearAll();How to know if the data come from Cache
if (null!= response.headers.value(DIO_CACHE_HEADER_KEY_DATA_SOURCE)) {// data come from cache}else {// data come from net}
_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.
Copyright 2019 HurshiLicensed 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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.About
http cache lib for Flutter dio like RxCache
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- Dart90.4%
- Ruby4.4%
- Objective-C2.3%
- Shell1.9%
- Java1.0%