weak_cache 2.0.1 
weak_cache: ^2.0.1 copied to clipboard
Weak cache that uses weak references for holding values. Implements full Map interface including keys and values iteration.
Weak Cache #
Weak cache is a Map implementation that uses WeakReferences for holding
values and Finalizer to manage it's storage.
You can use it to cache data (e.g. API responses) for a small amount of time (until next garbage collection cycle).
Features #
- Uses 
WeakReferencefor storing values. - Uses 
Finalizerto 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 
containsValuevia internal managedExpando. - Implements full 
Map<K, V>interface. - WeakCache itself can be safely garbage collected and doesn't hold unto any stored.
 
Usage #
Just create cache, add some values, and they'll be removed when all other strong references to they are lost.
// 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.