onDidRelease method

  1. @override
Future<Null> onDidRelease(
  1. TIdentifier id,
  2. TValue value,
  3. Future<Null> remove(
    1. TIdentifier id
    )
)
override

Custom logic to be executed after a Cache.release.

Cache.release awaits the completion of a pending value factory associated with the given TIdentifier before the onDidRelease lifecycle method is called.

Cache.release indicates that the TIdentifier, TValue pair have been marked as as eligible for removal from the cache. Whether the value is actually removed is determined here. The remove callback will remove the given item from the cache. For example the following implementation would set an eviction timer of 30 seconds.

@override
Future<Null> onDidRelease(TIdentifier id, TValue value, Future<Null>
    remove(TIdentifier id)) async {
  var timer = getManagedTimer(new Duration(seconds:30), () {
    remove(id);
  });
}

Values should not be removed unnecessarily. In the example below it is expected that both a, b and c would resolve to the same value while only making _superLongAsyncCall once. Here Cache.release and Cache.getAsync are called synchronously before _superLongAsyncCall has completed. The strategy has enough information to know that even though release was called there is no need for the value to be evicted and recomputed (because of the get immediately after the release). Release is a suggestion, every release does not need to be paired with a removal. If consumers wanted _superLongAsyncCall in the example below to be run twice, Cache.remove could be used.

var a = cache.getAsync('id', _superLongAsyncCall);
var release = cache.release('id');
var b = cache.getAsync('id', _superLongAsyncCall);

await release;
// _superLongAsyncCall and onDidRelease have now completed

var c = cache.getAsync('id', _superLongAsyncCall);

Implementation

@override
Future<Null> onDidRelease(
    TIdentifier id, TValue value, Future<Null> remove(TIdentifier id)) async {
  // If there are more than _keep items in the queue remove the least recently
  // used.
  while (_removalQueue.length > _keep) {
    await remove(_removalQueue.removeLast());
  }
}