这是indexloc提供的服务,不要输入任何密码

resource_reaper 1.0.5 copy "resource_reaper: ^1.0.5" to clipboard
resource_reaper: ^1.0.5 copied to clipboard

A lightweight resource manager for Dart and Flutter applications that handles the lifecycle and disposal of resources. Helps prevent memory leaks when caching data.

A lightweight resource manager for Dart and Flutter applications that handles the lifecycle and disposal of resources. Automatically purges expired items, disposes of old resources when capacity is reached, and helps prevent memory leaks.

Features #

✅ Automatic Disposal: Dispose of items when exceeding a maximum size limit.

⏱️ Time-Based Purging: Automatically purge items after a configurable expiration duration.

🗑️ Manual Control: Clear, purge, or remove items manually when needed.

⚡ Lightweight & Simple: Minimal dependencies, easy to integrate into existing projects.

Usage #

import 'package:resource_reaper/resource_reaper.dart';

class Connection {
  final String id;
  Connection(this.id);
  void close() => print('Connection $id closed.');
}

void main() {
  final reaper = ResourceReaper<Connection>(
    name: 'ConnectionReaper',
    size: 3,
    onDisposeItem: (conn) => conn.close(),
    verbose: true,
  );

  reaper.add(Connection('A'));
  reaper.add(Connection('B'));
  reaper.add(Connection('C'));
  reaper.add(Connection('D')); // Exceeds limit, disposes Connection A

  reaper.dispose(); // Clean up when done
}

For example usage with riverpod, refer to this example.

API Overview #

Parameter Type Required Description
name String ✅ Yes Identifier for the Reaper, useful for logging and debugging.
size int ✅ Yes Maximum number of items before the oldest ones are automatically disposed.
onDisposeItem void Function(T item) ✅ Yes Callback invoked when an item is disposed.
itemDuration Duration? ❌ No Duration after which items expire and are eligible for purging.
purgeInterval Duration? ❌ No Frequency at which expired items are purged. Defaults to itemDuration if not set.
verbose bool ❌ No Enables detailed debug logging if set to true. Defaults to false.
onFirstItemAdded void Function()? ❌ No Optional callback triggered when the first item is added to an empty Reaper.
onLastItemRemoved void Function()? ❌ No Optional callback triggered when the last item is removed from the Reaper.

Important Notes #

  • Always call dispose() when the ResourceReaper is no longer needed to prevent memory leaks due to lingering timers.
  • For custom classes, ensure == and hashCode are properly overridden for accurate item removal.
3
likes
160
points
70
downloads

Publisher

verified publisherdevxpress.ca

Weekly Downloads

A lightweight resource manager for Dart and Flutter applications that handles the lifecycle and disposal of resources. Helps prevent memory leaks when caching data.

Repository (GitHub)
View/report issues

Topics

#cache #state-management #riverpod #keep-alive #disposal

Documentation

API reference

License

MIT (license)

Dependencies

clock

More

Packages that depend on resource_reaper