cacherine 2.4.0 copy "cacherine: ^2.4.0" to clipboard
cacherine: ^2.4.0 copied to clipboard

A Dart in-memory cache library with FIFO, LRU, MRU, LFU, TTL expiry, async-safe variants, and monitoring metrics.

example/cacherine_example.dart

import 'package:cacherine/cacherine.dart';

Future<void> main() async {
  synchronousTTLExample();
  await asyncTTLExample();
  await cacheAsideExample();
  await monitoredCacheExample();
}

void synchronousTTLExample() {
  final cache = SimpleTTLCache<String, String>(
    ttl: const Duration(minutes: 5),
    maxSize: 100,
  );

  cache.set('token', 'abc123');
  cache.set('rate', '42', ttl: const Duration(seconds: 30));

  print('SimpleTTLCache token: ${cache.get('token')}');
  print('SimpleTTLCache has token: ${cache.containsKey('token')}');
}

Future<void> asyncTTLExample() async {
  final cache = TTLCache<String, String>(
    ttl: const Duration(minutes: 5),
    maxSize: 100,
  );

  await cache.set('user:1', 'Ada');
  await cache.set('session:1', 'active', ttl: const Duration(minutes: 1));

  print('TTLCache user: ${await cache.get('user:1')}');

  cache.dispose();
}

Future<void> cacheAsideExample() async {
  final simpleCache = SimpleLRUCache<String, String>(10);
  final syncValue = simpleCache.getOrSet('profile:1', () => 'Ada');

  final ttlCache = TTLCache<String, String>(
    ttl: const Duration(minutes: 5),
    maxSize: 10,
  );
  final asyncValue = await ttlCache.getOrCompute(
    'session:1',
    () async => 'active',
    ttl: const Duration(minutes: 1),
  );

  print('SimpleLRUCache computed value: $syncValue');
  print('TTLCache computed value: $asyncValue');

  ttlCache.dispose();
}

Future<void> monitoredCacheExample() async {
  final cache = MonitoredLRUCache<String, String>(maxSize: 2);

  await cache.set('a', 'first');
  await cache.set('b', 'second');
  await cache.get('a');
  await cache.get('missing');

  final dashboard = CacheStatsDashboard(cache.metrics);
  final snapshot = dashboard.snapshot(const Duration(minutes: 1));

  print(formatDashboard(snapshot));
  cache.dispose();
}
2
likes
160
points
4.44k
downloads

Documentation

API reference

Publisher

verified publisheryom-engine.com

Weekly Downloads

A Dart in-memory cache library with FIFO, LRU, MRU, LFU, TTL expiry, async-safe variants, and monitoring metrics.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

synchronized

More

Packages that depend on cacherine