AsyncCanonicalizingFetcher<K, V> class abstract

Helper to fetch a value asynchronously, where multiple outstanding requests for the same resource are canonicalized to a single request. This can prevent loading the same resource more than once.

Consider, for example, a cache of a resource loaded over the network, using the following algorithm:

Future<Thing> get(Key key) {
  if (cache has an entry for key) {
    return cache[key];
  }
  Fetch Thing t over network;
  Store t at cache[key];
  return t;
}

If a resource associated with some key k is being fetched over the network, and a second request for an equivalent key k2 comes in while the network operation for k is ongoing, the above algorithm would fetch the resource twice. This can be avoided by using get from a subclass of AsyncCanonicalilzingFetcher<Thing, Key> that implements create to load a Thing over the network, like this:

final fetcher = MyFetcher();

Future<Thing> get(Key key) => fetcher.get(key);

class MyFetcher extends AsyncCanonicalizingFetcher<Key, Thing> {

  @override
  Future<Thing> create(Key key) {
    if (cache has an entry for key) {
      return cache[key];
    }
    Fetch Thing t over network;
    Store t at cache[key];
    return t;
  }
}

A sample usage can be found in the jovial_svg caching sample.

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

create(K key) Future<V>
Create a value given key. This method is to be overridden by subclasses.
get(K key) Future<V>
Get the value identified by key. If this is called with a key that is equivalent to one that is currently being loaded, a identical Future identical to the one returned for the prior call will be returned.
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