cache_annotations 1.0.10
cache_annotations: ^1.0.10 copied to clipboard
This package is a type conversion generator using source_gen and inspired by Retrofit to help you manage persistent cache.
This package is a type conversion generator using source_gen and inspired by Retrofit to help you manage persistent cache.
Usage #
Add the annotations and generators to your dependencies
flutter pub add cache_annotations
flutter pub add dev:cache_generators
Or add it manually to your pubspec.yaml
dependencies:
cache_annotations: ^1.0.9
dev_dependencies:
build_runner: ^2.4.15
cache_generators: ^1.0.9
Define your cache #
import 'package:cache_annotations/annotations.dart';
import 'package:cache_generator_example/user.dart';
part 'cache.g.dart';
@LocalStoreCache('my_local_store_cache')
abstract class Cache with LocalStoreCacheMixIn {
static final Cache _instance = _Cache();
static Cache get instance => _instance;
@persistent
@Cached(path: 'device_id')
CacheEntry<Iterable<String>> deviceId();
@Cached(fromJson: User.fromJson, toJson: userToJson)
CacheEntry<User> me();
@MaxAge(Duration(seconds: 2))
@Cached(path: 'friends/{id}')
CacheEntry<String> friendById(
@Path('id') int userId,
);
@Cached(path: 'likes/{date}')
CacheEntry<String> likes(
@Path('date', convert: keyDateConvertor) DateTime date,
);
}
Run the generator #
# dart
dart pub run build_runner build
# flutter
flutter pub run build_runner build
Use it #
Cache cache = Cache.instance;
await cache.deviceId().set(['dummy', 'ok']);
print(await cache.deviceId().get());
await cache.me().set(User('Someone', 26));
print(await cache.me().get());
await cache.friendById(12).set('Joe');
print(await cache.friendById(12).get());
# Get all documents in a specific collection
final Iterable<String>? all = await cache.all(
'friends',
fromJson: (e) => e as String,
);
# Delete a specific collection. Also support isPersistent parameter
await cache.deleteCollection('friends');
# Delete all (except persistent)
await cache.deleteAll();
# Delete all including persistent
await cache.deleteAll(deletePersistent: true);
Additional information #
If you find a bug or want a feature, please file an issue on github Here.