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. [...]
factory
Properties
- hashCode → int
-
The hash code for this object. [...]
read-only, inherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
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 non-existent method or property is accessed. [...]
inherited
-
toString(
) → String -
Returns a string representation of this object.
inherited
Operators
-
operator ==(
dynamic other) → bool -
The equality operator. [...]
inherited