shared_preferences_cache 0.1.0+3 copy "shared_preferences_cache: ^0.1.0+3" to clipboard
shared_preferences_cache: ^0.1.0+3 copied to clipboard

A Cache for Flutter based on Shared Preferences to cache results of heavy calcuations or API calls.

shared_preferences_cache #

A Cache for Flutter based on Shared Preferences. It is similar to the package flutter_cache_manager but instead of caching cache downloaded files, shared_preferences_cache can cache plain values (int, double, string, bool). This comes in handy if you want to cache a result of an expensive operation or of an API call which cannot be cached with flutter_cache_manager.

Usage #

To use this plugin, add shared_preferences_cache as a dependency in your pubspec.yaml file.

Example #

import 'package:shared_preferences_cache/shared_preferences_cache.dart';

void main() async {
      SharedPreferencesCache spc =
          await SharedPreferencesCache.getInstance(Duration(minutes: 5));

      Future<int> Function() heavyFunction = () async {
        // This code will only be executed if...
        // 1. No value for the given key exists in the cache
        // or
        // 2. The cached value is older then the provided maxAge (5 days in this example)

        print('Doing some heavy calculations');
        return 42;
      };

      print(await spc.getInt('the_answer', heavyFunction));
      sleep(Duration(seconds: 1));
      print(await spc.getInt('the_answer', heavyFunction));
}

To Do #

  • Proper API documentation

Known Issues #

  • The library is not thread-safe. When used in parallel it can happen that the key and value pairs fall apart
3
likes
40
pub points
0%
popularity

Publisher

unverified uploader

A Cache for Flutter based on Shared Preferences to cache results of heavy calcuations or API calls.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (LICENSE)

Dependencies

flutter, shared_preferences, shared_preferences_platform_interface

More

Packages that depend on shared_preferences_cache