搜索插件、代码仓与 APP 分类

插件详情v0.3.0

dio_http_cache

适用于 Flutter 的 Dio HTTP 缓存库,类似于 RxCache。它使用 sqflite 作为磁盘缓存,并使用 google/quiver-dart/LRU 策略作为内存缓存。

AndroidiOSmacOS
近 30 天下载
1550
喜欢数
248
Pub 分数
120

安装插件

flutter pub add dio_http_cache

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Dio HTTP Cache 是 Flutter 的 Dio HTTP 客户端的缓存库,提供内存和磁盘两种缓存方式。它使用 sqflite 实现磁盘上的持久化存储,并通过 google/quiver-dart 实现 LRU(最近最少使用)策略,以实现高效的内存管理。该包可通过缓存 HTTP 响应来减少重复的网络请求,提升应用性能并节省带宽。适用于需要离线支持或频繁调用稳定数据 API 的应用。

适合场景

  • 缓存 API 响应以减少网络使用
  • 启用对先前获取数据的离线访问
  • 通过降低延迟来提升应用性能
  • 高效存储频繁访问的数据
  • 通过缓存响应减少服务器负载

主要特点

  • 使用 sqflite 的磁盘缓存
  • LRU内存缓存,带quiver-dart
  • 自动请求/响应缓存
  • 可配置的缓存过期
  • 支持 GET 和 HEAD 请求

适合对象

  • 频繁调用API的应用
  • 离线可用的应用程序
  • 性能优化的网络层
  • 数据密集型移动应用
  • 使用 Dio 进行 HTTP 请求的开发者

使用建议

  • 需要 sqflite 设置和权限
  • 缓存大小必须手动管理
  • 不适合大型二进制数据
  • 仅限 GET/HEAD 方法
  • 可能需要为复杂请求自定义缓存键逻辑

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

dio-http-cache

Pub

中文介绍

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
dependencies:
  dio_http_cache: ^0.3.x #latest version
QuickStart
  1. Add a dio-http-cache interceptor in Dio :

    dio.interceptors.add(DioCacheManager(CacheConfig(baseUrl: "http://www.google.com")).interceptor);
    
  2. Set maxAge for a request :

    Dio().get(
      "http://www.google.com",
      options: buildCacheOptions(Duration(days: 7)),
    );
    
The advanced
  1. Custom your config by buildCacheOptions :

    1. primaryKey: By default, host + path is used as the primaryKey, and you can also customize it.

    2. 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")
      
    3. maxAge: set the cache time. If the value is null or not setted, it will try to get maxAge and maxStale from response headers.

    4. 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))
      
    5. forceRefresh: false default.

      buildCacheOptions(Duration(days: 7), forceRefresh: true)
      
      1. Get data from network first.
      2. If getting data from network succeeds, store or refresh cache.
      3. If getting data from network fails or no network avaliable, try get data from cache instead of an error.
  2. Use "CacheConfig" to config default params

    1. 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".
    2. encrypt / decrypt: these two must be used together to encrypt the disk cache data, you can also zip data here.
    3. defaultMaxAge: use Duration(day:7) as default.
    4. defaultaMaxStale: similar with DefaultMaxAge.
    5. databasePath: database path.
    6. databaseName: database name.
    7. skipMemoryCache: false defalut.
    8. skipDiskCache: false default.
    9. maxMemoryCacheCount: 100 defalut.
    10. defaultRequestMethod: use "POST" as default, it will be used in delete caches.
    11. diskStore: custom disk storage.
  3. How to clear expired cache

    • Just ignore it, that is automatic.

    • But if you insist : DioCacheManager.clearExpired();

  4. How to delete caches

    1. No matter what subKey is, delete local cache if primary matched.

      // Automatically parses primarykey from path
      _dioCacheManager.deleteByPrimaryKey(path, requestMethod: "POST"); 
      
    2. 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}); 
      
    3. 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});
      
  5. How to clear All caches (expired or not)

    _dioCacheManager.clearAll();
    
  6. 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
    }
    
Example for maxAge and maxStale
_dio.post(
	"https://www.exmaple.com",
	data: {'k': "keyword"},
	options:buildCacheOptions(
  		Duration(days: 3), 
  		maxStale: Duration(days: 7), 
	)
)
  1. 0 ~ 3 days : Return data from cache directly (irrelevant with network).
  2. 3 ~ 7 days:
    1. Get data from network first.
    2. If getting data from network succeeds, refresh cache.
    3. If getting data from network fails or no network avaliable, try get data from cache instead of an error.
  3. 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.

所属分类

依赖

依赖清单 7 项

源码仓库

使用此插件的项目