cache_it 0.0.5
cache_it: ^0.0.5 copied to clipboard

A simple object cache implementation

cache_it #

A simple cache package.

Install Dependencies #

Add the following to your pubspec.yaml:

dependencies:
  cache_it:
    git:
      url: https://github.com/berbsd/flutter-cache.git

Add a Cache<K,V> object to your class by specifying the key (K) and the value type V.

import 'package:cache_it/cache_it.dart';

class ActivityRepository {
  // instantiate a cache with 10 min ttl for objects
  final CacheIt<int, Activity> _cache = CacheIt<int, Activity>(ttl: 600);

  Activity getActivityById(int id) async {
    // retrieving a cached value, returns null if absent or expired
    return _cache.getOrUpdate(id, builder: () => await http.get('$url/$id'));
}

Other example

import 'package:cache_it/cache_it.dart';

class ActivityRepository {
  // instantiate a cache with 10 min ttl for objects
  final CacheIt<int, Activity> _cache = CacheIt<int, Activity>(ttl: 600);

  Activity getActivityById(int id) async {
    // retrieving a cached value, returns null if absent or expired
    Activity activity = _cache.get(id);
    if(activity == null) {
      // fetch a new value from API
      activity = await http.get('$url/$id');
      // cache the newly retrieved value
      _cache.add(id, activity);
    }
    return activity;
  }
}
0
likes
70
pub points
0%
popularity

A simple object cache implementation

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

berbsd@gmail.com

License

MIT (LICENSE)

Dependencies

uuid

More

Packages that depend on cache_it