【发布时间】:2016-01-18 09:23:57
【问题描述】:
在我的 GWT 应用程序中,我经常多次引用相同的服务器结果。我也不知道先执行哪个代码。因此,我想对我的异步(客户端)结果进行缓存。
我想使用现有的缓存库;我正在考虑 guava-gwt。
我发现了这个 Guava 同步缓存的例子(guava's documentation):
LoadingCache<Key, Graph> graphs = CacheBuilder.newBuilder()
.build(
new CacheLoader<Key, Graph>() {
public Graph load(Key key) throws AnyException {
return createExpensiveGraph(key);
}
});
这就是我尝试异步使用 Guava 缓存的方式(我不知道如何使这项工作发挥作用):
LoadingCache<Key, Graph> graphs = CacheBuilder.newBuilder()
.build(
new CacheLoader<Key, Graph>() {
public Graph load(Key key) throws AnyException {
// I want to do something asynchronous here, I cannot use Thread.sleep in the browser/JavaScript environment.
service.createExpensiveGraph(key, new AsyncCallback<Graph>() {
public void onFailure(Throwable caught) {
// how to tell the cache about the failure???
}
public void onSuccess(Graph result) {
// how to fill the cache with that result???
}
});
return // I cannot provide any result yet. What can I return???
}
});
GWT 缺少默认 JRE 中的许多类(尤其是在线程和并发方面)。
如何使用 guava-gwt 缓存异步结果?
【问题讨论】: