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.
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:
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.
请发表评论