vit_cache 1.1.1
vit_cache: ^1.1.1 copied to clipboard
A Dart package for caching single values and multiple key-value pairs with a time-to-live (TTL) mechanism.
example/vit_cache_example.dart
import 'package:vit_cache/vit_cache.dart';
class UserInfoCache extends SingularCache<Map<String, dynamic>> {
@override
Future<Map<String, dynamic>> fetch() async {
// Simulate a network call to fetch user info
await Future.delayed(Duration(seconds: 2));
return {'name': 'Dave', 'auth_token': 'xxxxx'};
}
@override
Duration get ttl => Duration(seconds: 10);
}
void main() async {
var userInfoCache = UserInfoCache();
// Fetch and cache user info
var info = await userInfoCache.get();
print('User Info: $info');
// Cache hit
info = await userInfoCache.get();
print('User Info: $info');
// Update the cached value manually
userInfoCache.update({'name': 'Dave', 'auth_token': 'xxxxx'});
// Clear the cache
userInfoCache.clear();
}