weak_cache 2.1.3 copy "weak_cache: ^2.1.3" to clipboard
weak_cache: ^2.1.3 copied to clipboard

Weak cache that uses weak references for holding values. Implements full Map interface including keys and values iteration.

Weak Cache #

pub package package publisher

Weak cache is a Map implementation that uses WeakReferences for holding values and Finalizer to manage it's storage.

You can use this to cache data for a small amount of time until next garbage collection cycle.

Note: Values cannot be numbers, strings, booleans, records, null, dart:ffi pointers, dart:ffi structs, or dart:ffi unions.

Features #

  • Uses WeakReference for storing values.
  • Uses Finalizer to remove objects from internal storage upon their deletion.
  • Allows you iterate over keys/values.

    While iterating, all stored values are temporarily made into strong references, to prevent concurrent edit of storage, while iterating over it.

  • Optimized containsValue via internal managed Expando.
  • Implements full Map<K, V> interface.
  • WeakCache itself can be safely garbage collected and doesn't hold unto any stored data.

Usage #

Create cache, add values, and they'll be removed once there no more strong references to them.

// ID - Object cache
final cache = WeakCache<int, Object>();

Object? obj = Object();
cache[0] = obj;
// ...
obj = null;
// ...
// After garbage collection cache[0] will be removed.
cache[0] == null;

See example for detailed test case.

3
likes
140
points
86
downloads

Publisher

verified publishernull.moe

Weekly Downloads

Weak cache that uses weak references for holding values. Implements full Map interface including keys and values iteration.

Repository (GitHub)
View/report issues

Topics

#cache #memory

Documentation

API reference

License

ISC (license)

Dependencies

meta

More

Packages that depend on weak_cache