ttl_cache 0.3.0 ttl_cache: ^0.3.0 copied to clipboard
A simple key-value store whose entires expire after a user-defined time-to-live (TTL)
// ignore_for_file: avoid_print
import 'package:ttl_cache/ttl_cache.dart';
Future<void> main() async {
print('creating cache with default TTL of 1 second');
final cache = TtlCache<String, String>(
defaultTtl: const Duration(seconds: 1),
);
print('inserting entries:');
print('foo: bar (default TTL)');
print('hello: world (2 second TTL)');
print('');
cache['foo'] = 'bar';
cache.set('hello', 'world', ttl: const Duration(seconds: 2));
print('initial state:');
print('foo: ${cache['foo']}'); // 'bar'
print('hello: ${cache.get('hello')}'); // 'world'
print('');
await Future<void>.delayed(const Duration(seconds: 1));
print('after 1 second:');
print('foo: ${cache['foo']}'); // null
print('hello: ${cache.get('hello')}'); // 'world'
print('');
await Future<void>.delayed(const Duration(seconds: 1));
print('after 2 seconds:');
print('foo: ${cache['foo']}'); // null
print('hello: ${cache.get('hello')}'); // null
}