pooled_map 1.0.0 pooled_map: ^1.0.0 copied to clipboard
An asynchronous map for Dart that locks keys, to avoid the Thundering Herd problem.
pooled_map #
An asynchronous map for Dart that locks keys, to avoid the Thundering Herd problem.
package:pooled_map
is best-suited for implementing caches of expensive
operations (i.e. database lookups).
Notes #
PooledMap
is inherently asynchronous; therefore, it is incompatible with the standardMap
. However, you can easily convert between the two.PooledMap
by default haslockEntireMap
set tofalse
, as this can become a bottleneck for Web applications. If you plan to frequently querylength
,keys
,values
, orclear
the map, however, you may consider enabling it. Most applications will likely not need it.
Brief Example #
import 'dart:async';
import 'package:pooled_map/pooled_map.dart';
main() async {
// Even though we start 100000 simultaneous, asynchronous updates,
// access is restricted to one-at-a-time.
//
// Thus, the output will be 100000.
var i = 100000;
var map = new PooledMap<String, int>(lockEntireMap: true);
Future<int> increment() {
return map.update('i', (i) => i + 1, defaultValue: () => 0);
}
var futures = new List<Future<int>>.generate(i, (_) => increment());
await Future.wait(futures);
var value = await map['i'];
print('$value == 100000');
}