- Notifications
You must be signed in to change notification settings - Fork0
Generic cache manager for flutter
License
a-benamer/flutter_cache_manager
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
BREAKING CHANGES IN V2
CacheManager v2 introduced some breaking changes when configuring a custom CacheManager.See the bottom of this pagefor the changes.
A CacheManager to download and cache files in the cache directory of the app. Various settings on how long to keep a file can be changed.
It uses the cache-control http header to efficiently retrieve files.
The more basic usage is explained here. See the complete docs for more info.
The cache manager can be used to get a file on various waysThe easiest way to get a single file is call.getSingleFile.
var file=awaitDefaultCacheManager().getSingleFile(url);
getFileStream(url) returns a stream with the first result being the cached file and later optionally the downloaded file.
getFileStream(url, withProgress: true) when you set withProgress on true, this stream will also emit DownloadProgress when the file is not found in the cache.
downloadFile(url) directly downloads from the web.
getFileFromCache only retrieves from cache and returns no file when the file is not in the cache.
putFile gives the option to put a new file into the cache without downloading it.
removeFile removes a file from the cache.
emptyCache removes all files from the cache.
If you use the ImageCacheManager mixin on the CacheManager (which is already done on the DefaultCacheManager) youget the followinggetImageFile method for free:
Stream<FileResponse>getImageFile(String url, {String key,Map<String,String> headers,bool withProgress,int maxHeight,// This is extraint maxWidth,// This is extra as well})
The image from the url is resized within the specifications, and the resized images is stored in the cache. Italways tries to keep the existing aspect ratios. The original image is also cached and used to resize the image ifyou call this method with other height/width parameters.
When your files are stored on Firebase Storage you can useflutter_cache_manager_firebase.
The cache manager is customizable by creating a new CacheManager. It is very important to not create more than 1CacheManager instance with the same key as these bite each other. In the example down here the manager is created as aSingleton, but you could also use for example Provider to Provide a CacheManager on the top level of your app.Below is an example with other settings for the maximum age of files, maximum number of objectsand a custom FileService. The key parameter in the constructor is mandatory, all other variables are optional.
classCustomCacheManager {staticconst key='customCacheKey';staticCacheManager instance=CacheManager(Config( key, stalePeriod:constDuration(days:7), maxNrOfCacheObjects:20, repo:JsonCacheInfoRepository(databaseName: key), fileSystem:IOFileSystem(key), fileService:HttpFileService(), ), );}
By default the cached files are stored in the temporary directory of the app. This means the OS can delete the files any time.
Information about the files is stored in a database using sqflite on Android, iOS and macOs, or in a plain JSON fileon other platforms. The file name of the database is the key of the cacheManager, that's why that has to be unique.
A valid url response should contain a Cache-Control header. More info on the header can be foundhere, but in summary it says for how longthe image can be expected to be up to date. It also contains an 'eTag' which can be used to check (after that time)whether the file did change or if it is actually still valid.
When a file is in the cache that is always directly returned when callinggetSingleFile orgetFileStream.After that the information is check if the file is actually still valid. If the file is outdated according to theCache-Control headers the manager tries to update the file and store the new one in the cache. When you usegetFileStream this updated file will also be returned in the stream.
The files can be removed by the cache manager or by the operating system. By default the files are stored in a cachefolder, which is sometimes cleaned for example on Android with an app update.
The cache manager uses 2 variables to determine when to delete a file, themaxNrOfCacheObjects and thestalePeriod.The cache knows when files have been used latest. When cleaning the cache (which happens continuously), the cachedeletes files when there are too many, ordered by last use, and when files just haven't been used for longer thanthe stale period.
There is no longer a need to extend on BaseCacheManager, you can directly call the constructor. The BaseCacheManageris now only an interface. CacheManager is the implementation you can use directly.
The constructor now expects a Config object with some settings you were used to, but some are slightly different.For example the system where you want to store your files is not just a dictionary anymore, but a FileSystem. That wayyou have more freedom on where to store your files.
See the example inCustomize.
About
Generic cache manager for flutter
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Languages
- Dart81.9%
- CMake8.4%
- C++8.0%
- Ruby1.2%
- HTML0.5%