我目前有一个类,它在通过GRPC异步获取图像后存储图像。在处理事件时有一个问题。问题可以在这里显示:
import 'dart:async';
main() {
IntStore intStore = IntStore();
print("running getInt(A)");
intStore.getInt("A");
print("running getInt(A)");
intStore.getInt("A");
print("running getInt(B)");
intStore.getInt("B");
print("running getInt(A)");
intStore.getInt("A");
print("running getInt(C)");
intStore.getInt("C");
print("running getInt(D)");
intStore.getInt("D");
}
class IntStore {
final Map _store = <String, int>{};
Future fetchInt(String intName) async {
print("Fetching: $intName");
await doSomeWorkAsynchronously(intName);
}
Future<int> getInt(String intName) async {
if (_store.containsKey(intName)) {
print("Cached: $intName");
return _store[intName];
} else {
await fetchInt(intName);
return _store[intName];
}
}
Future doSomeWorkAsynchronously(String intName) async {
await Future.delayed(const Duration(seconds: 3));
_store[intName] = 3;
print("Fetched: $intName");
}
}
返回:
running getInt(A)
Fetching: A
running getInt(A)
Fetching: A
running getInt(B)
Fetching: B
running getInt(A)
Fetching: A
running getInt(C)
Fetching: C
running getInt(D)
Fetching: D
Fetched: A
Fetched: A
Fetched: B
Fetched: A
Fetched: C
Fetched: D
这里的问题是fetchInt中的工作完成了多次,这是相当低效的。
1条答案
按热度按时间rkttyhzu1#
我发现Irn在这个question中的答案很有帮助。我实现了这个方法以获得:
其返回
这里的问题是每个fetchInt调用,虽然可能是必要的,但会彼此阻塞。这比以前效率更低。我修改了实现以提高效率:
其返回
因此,它将尽可能快地返回每个int,保持不同调用之间的并发性,但阻止对同一int的重复调用。