https://github.com/zekfad/weak_cache
Cache powered by weak references for Dart
https://github.com/zekfad/weak_cache
cache dart dart-package weak-references weakref weakreference
Last synced: 28 days ago
JSON representation
Cache powered by weak references for Dart
- Host: GitHub
- URL: https://github.com/zekfad/weak_cache
- Owner: Zekfad
- License: isc
- Created: 2022-05-18T01:25:27.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-06-23T23:03:52.000Z (11 months ago)
- Last Synced: 2025-03-01T12:22:56.053Z (3 months ago)
- Topics: cache, dart, dart-package, weak-references, weakref, weakreference
- Language: Dart
- Homepage: https://pub.dev/packages/weak_cache
- Size: 38.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Weak Cache
[](https://pub.dev/packages/weak_cache)
[](https://pub.dev/packages/weak_cache/publisher)Weak cache is a `Map` implementation that uses `WeakReference`s 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` 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.```dart
// ID - Object cache
final cache = WeakCache();Object? obj = Object();
cache[0] = obj;
// ...
obj = null;
// ...
// After garbage collection cache[0] will be removed.
cache[0] == null;
```See [example](example/weak_cache_example.dart) for detailed test case.