Package selection guidance compiled from public package information, community data, and project usage.
Dio HTTP Cache is a caching library for Flutter's Dio HTTP client, providing both in-memory and disk-based caching. It uses sqflite for persistent storage on disk and implements an LRU (Least Recently Used) strategy via google/quiver-dart for efficient memory management. This package helps reduce redundant network requests, improve app performance, and save bandwidth by caching HTTP responses. Ideal for apps requiring offline support or frequent API calls with stable data.
Use cases
Caching API responses to reduce network usage
Enabling offline access to previously fetched data
Improving app performance with reduced latency
Storing frequently accessed data efficiently
Reducing server load through cached responses
Key features
Disk caching using sqflite
LRU memory cache with quiver-dart
Automatic request/response caching
Configurable cache expiration
Supports GET and HEAD requests
Suitable for
Apps with frequent API calls
Offline-capable applications
Performance-optimized network layers
Data-heavy mobile apps
Developers using Dio for HTTP requests
Considerations
Requires sqflite setup and permissions
Cache size must be managed manually
Not suitable for large binary data
Limited to GET/HEAD methods
May require custom cache key logic for complex requests
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
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.