cache_it

Creator: coderz1093

Last updated:

0 purchases

cache_it Image
cache_it Images

Languages

Categories

Add to Cart

Description:

cache it

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
copied to clipboard
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'));
}
copied to clipboard
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;
}
}
copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.