ttl_cache 0.3.0 copy "ttl_cache: ^0.3.0" to clipboard
ttl_cache: ^0.3.0 copied to clipboard

A simple key-value store whose entires expire after a user-defined time-to-live (TTL)

example/main.dart

// 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
}
2
likes
160
points
10.9k
downloads

Publisher

verified publisherbryanoltman.com

Weekly Downloads

A simple key-value store whose entires expire after a user-defined time-to-live (TTL)

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

clock

More

Packages that depend on ttl_cache