Dio HTTP Cache는 Flutter의 Dio HTTP 클라이언트용 캐시 라이브러리로, 메모리 및 디스크 기반 캐싱을 모두 제공합니다. 디스크에 지속적인 저장을 위해 sqflite를 사용하며, google/quiver-dart를 통해 LRU(최근 사용된 순서 기준) 전략을 구현하여 효율적인 메모리 관리를 합니다. 이 패키지는 HTTP 응답을 캐시함으로써 중복 네트워크 요청을 줄이고 앱 성능을 향상시키며 대역폭을 절약하는 데 도움을 줍니다. 오프라인 지원이 필요하거나 안정적인 데이터를 가진 자주 호출되는 API가 있는 앱에 적합합니다.
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
Copyright2019HurshiLicensed under the ApacheLicense, Version2.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.0Unlessrequired by applicable law or agreed to in writing, software
distributed under the Licenseis distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Licensefor the specific language governing permissions and
limitations under the License.