• Stars
    star
    749
  • Rank 60,575 (Top 2 %)
  • Language
    Dart
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generic cache manager for flutter

BREAKING CHANGES IN V2

CacheManager v2 introduced some breaking changes when configuring a custom CacheManager. See the bottom of this page for the changes.

flutter_cache_manager

pub package Build Status codecov

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.

Usage

The cache manager can be used to get a file on various ways The easiest way to get a single file is call .getSingleFile.

    var file = await DefaultCacheManager().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.

Other implementations

When your files are stored on Firebase Storage you can use flutter_cache_manager_firebase.

Customize

The cache manager is customizable by creating a new CacheManager. It is very important to not create more than 1 CacheManager instance with the same key as these bite each other. In the example down here the manager is created as a Singleton, 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 objects and a custom FileService. The key parameter in the constructor is mandatory, all other variables are optional.

class CustomCacheManager {
  static const key = 'customCacheKey';
  static CacheManager instance = CacheManager(
    Config(
      key,
      stalePeriod: const Duration(days: 7),
      maxNrOfCacheObjects: 20,
      repo: JsonCacheInfoRepository(databaseName: key),
      fileSystem: IOFileSystem(key),
      fileService: HttpFileService(),
    ),
  );
}

Frequently Asked Questions

How are the cache files stored?

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 file on other platforms. The file name of the database is the key of the cacheManager, that's why that has to be unique.

When are the cached files updated?

A valid url response should contain a Cache-Control header. More info on the header can be found here, but in summary it says for how long the 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 calling getSingleFile or getFileStream. After that the information is check if the file is actually still valid. If the file is outdated according to the Cache-Control headers the manager tries to update the file and store the new one in the cache. When you use getFileStream this updated file will also be returned in the stream.

When are cached files removed?

The files can be removed by the cache manager or by the operating system. By default the files are stored in a cache folder, 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, the maxNrOfCacheObjects and the stalePeriod. The cache knows when files have been used latest. When cleaning the cache (which happens continuously), the cache deletes files when there are too many, ordered by last use, and when files just haven't been used for longer than the stale period.

Breaking changes in v2

  • There is no longer a need to extend on BaseCacheManager, you can directly call the constructor. The BaseCacheManager is therefore renamed to CacheManager as it is not really just a 'base' anymore.

  • 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 way you have more freedom on where to store your files.

More Repositories

1

PhotoView

Implementation of ImageView for Android that supports zooming, by various touch gestures.
Java
18,771
star
2

flutter_cached_network_image

Download, cache and show images in a flutter app
Dart
2,426
star
3

flutter-permission-handler

Permission plugin for Flutter. This plugin provides a cross-platform (iOS, Android) API to request and check permissions.
Dart
2,020
star
4

flutter-geolocator

Android and iOS Geolocation plugin for Flutter
Dart
1,239
star
5

LottieXamarin

Render After Effects animations natively on Android, iOS, MacOS and TvOS for Xamarin
C#
1,218
star
6

XamarinMediaManager

Cross platform Xamarin plugin to play and control Audio and Video
C#
766
star
7

XF-Material-Library

A Xamarin Forms library for implementing Material Design
C#
647
star
8

octo_image

A multifunctional Flutter image widget
Dart
156
star
9

ExoPlayerXamarin

Xamarin bindings library for the Google ExoPlayer library
C#
153
star
10

Chameleon

Chameleon is a flexible media player build with Xamarin.Forms
C#
152
star
11

flutter-geocoding

A Geocoding plugin for Flutter
Dart
135
star
12

Xamarin-Sidebar

A slideout navigation control for Xamarin.iOS
C#
113
star
13

screenrecorder

Flutter package which can be used to record flutter widgets
Dart
64
star
14

FoldingCell

FoldingCell is an expanding content cell inspired by folding paper material
C#
55
star
15

flutter-permission-plugins

This repo contains a collection of permission related Flutter plugins which can be used to request permissions to access device resources in a cross-platform way.
Dart
52
star
16

MvxForms

Sample App with Xamarin.Forms and MvvmCross
C#
47
star
17

HugoStructuredData

Collection of structured data snippets in Google preferred JSON-LD format, with support for Hugo
HTML
42
star
18

flutter-contacts-plugin

Contact plugin for Flutter. This plugin provides a cross-platform (iOS, Android) API to read, create and update contacts from the address book.
Dart
40
star
19

NavigationTabBarXamarin

Navigation tab bar with colorful interactions for Xamarin Android
C#
39
star
20

InfiniteCycleViewPagerXamarin

Infinite cycle ViewPager with two-way orientation and interactive effect.
C#
36
star
21

XamarinItemTouchHelper

Basic example of using ItemTouchHelper to add drag & drop and swipe-to-dismiss to RecyclerView for Xamarin
C#
36
star
22

VlcXamarin

Xamarin.Android bindings for VLC player
C#
34
star
23

flutter-google-api-availability

Check the availability of Google Play services on the current device
Dart
34
star
24

NavigationTabStripXamarin

Navigation tab strip with smooth interaction
C#
24
star
25

FantasySlideXamarin

Another sliding menu base on DrawerLayout
C#
20
star
26

DiagonalLayoutXamarin

With Diagonal Layout explore new styles and approaches on material design
C#
19
star
27

LoaderViewXamarin

Library that enables TextView of ImageView to show loading animation while waiting for the text and image get loaded
C#
17
star
28

flutter-video-bloc

Dart
16
star
29

flutter_wizard

A library that makes it easy for you to create your own custom wizard.
Dart
16
star
30

FloatingNavigationView

A simple Floating Action Button that shows an anchored Navigation View
C#
16
star
31

flutter-essentials

Essential cross platform APIs for your Flutter apps
Dart
16
star
32

MaterialDesignHelpers

Default colors and dimens per Material Design guidelines and Android Design guidelines inside one library.
C#
13
star
33

GuardedActions

A library to increase the error handling, testability and reusability for all your MVVM driven apps!
C#
13
star
34

HoverXamarin

A floating menu library for Xamarin Android.
C#
12
star
35

FlipShareXamarin

It's a flip way to show share widget.
C#
11
star
36

AndroidSlidingUpPanelXamarin

This library provides a simple way to add a draggable sliding up panel to your Android app
C#
11
star
37

byteplot

A Dart command-line tool for Flutter to make your life easier
Dart
10
star
38

FFmpegMediaMetadataRetrieverXamarin

FFmpegMediaMetadataRetriever provides a unified interface for retrieving frame and meta data from an input media file.
C#
10
star
39

full-stack-on-rust

Source code and documentation for our 'full stack on rust' meetup on 29-9-2022
Rust
8
star
40

InteractiveMediaAdsXamarin

IMA Android SDK v3 for Xamarin
C#
8
star
41

AutoLinkTextViewXamarin

AutoLinkTextView is TextView that supports Hashtags (#), Mentions (@) , URLs (http://), Phone and Email automatically detecting and ability to handle clicks.
C#
8
star
42

GoogleVRXamarin

Xamarin bindings library for the Google VR library
C#
8
star
43

cheat-sheets

This repo contains a set of cheat sheets often used by the Baseflow crew.
8
star
44

flutter-blues

Flutter plugin to provide easy access to platform specific Bluetooth low energy services
Dart
7
star
45

flutter_swipe_detector

A Flutter package to detect your swipe directions and provides you with callbacks to handle them.
Dart
7
star
46

flutter_uwb

A Flutter plugin for working with Ultra Wide Band sensors.
Dart
7
star
47

stellar-rust-sdk

Welcome to the Rust Stellar SDK repository! This project aims to empower developers with a robust Rust SDK for the Stellar cryptocurrency network. Leverage Rust's performance and security advantages to build efficient and scalable applications on Stellar. Join us in shaping the future of blockchain development with Stellar and Rust integration.
Rust
7
star
48

WaveSideBarXamarin

An Index Side Bar With Wave Effect
C#
6
star
49

baseflow_plugin_template

Template for the Baseflow flutter plugins
Dart
5
star
50

VerticalViewPagerXamarin

Vertically ViewPager and vertically transformer for Xamarin Android
C#
5
star
51

flutter_state_management_demo

A demonstration app showcasing the Baseflow way of State Management in Flutter
Dart
4
star
52

AutoscaleEditTextXamarin

AutoscaleEditText bindings for Xamarin Android
C#
4
star
53

flutter-style-guide

A Flutter style-guide example app
Dart
3
star
54

DSTV.Net

A basic Tekla DSTV NC file parser written in C#
C#
3
star
55

flutter-meetup

Sheets and samples for our flutter meetups
Dart
3
star
56

konami_detector

A package to detect your konami codes and executes the provided callbacks.
Dart
3
star
57

terraform-demo

Code and descriptions for the demo on the 14th July 2023
HCL
2
star
58

service_manager

A Flutter plugin to manage the state of platform specific services (e.g. location services).
Dart
2
star
59

flutter_ioc

A standard interface providing inversion of control services to Dart or Flutter applications.
Dart
2
star
60

EllipsizeTextViewXamarin

The EllipsizeTextView extends TextView, support omit (Ellipsize/Ellipsis) redundant characters in multiple lines situtation.
C#
2
star
61

GuardedActions.Samples

Contains sample project on how to implement the GuardedActions package.
C#
1
star