AsyncCache<T> class
Runs asynchronous functions and caches the result for a period of time.
This class exists to cover the pattern of having potentially expensive code such as file I/O, network access, or isolate computation that's unlikely to change quickly run fewer times. For example:
final _usersCache = new AsyncCache<List<String>>(const Duration(hours: 1));
/// Uses the cache if it exists, otherwise calls the closure:
Future<List<String>> get onlineUsers => _usersCache.fetch(() {
// Actually fetch online users here.
});
This class's timing can be mocked using
fake_async
.
Constructors
- AsyncCache(Duration duration)
-
Creates a cache that invalidates its contents after
duration
has passed. - AsyncCache.ephemeral()
- Creates a cache that invalidates after an in-flight request is complete.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
fetch(
Future< T> callback()) → Future<T> -
Returns a cached value from a previous call to fetch, or runs
callback
to compute a new one. -
fetchStream(
Stream< T> callback()) → Stream<T> -
Returns a cached stream from a previous call to fetchStream, or runs
callback
to compute a new stream. -
invalidate(
) → void - Removes any cached value.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited