gpt4 book ai didi

java - 如何使用 Guava 在 GWT 中缓存服务器结果?

转载 作者:搜寻专家 更新时间:2023-10-30 21:32:44 25 4
gpt4 key购买 nike

在我的 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 缓存异步结果?

最佳答案

据我了解,您想要实现的不仅是异步缓存,而且是惰性缓存,创建一个 GWT 不是最佳位置,因为在使用客户端异步执行实现 GWT 应用程序时存在大问题,因为 GWT 缺少 Future 和/或 Rx 组件的客户端实现(仍然有一些用于 GWT 的 RxJava 实现)。所以在通常的 java 中,你想要创建的东西可以通过以下方式实现:

LoadingCache<String, Future<String>> graphs = CacheBuilder.newBuilder().build(new CacheLoader<String, Future<String>>() {
public Future<String> load(String key) {
ExecutorService service = Executors.newSingleThreadExecutor();
return service.submit(()->service.createExpensiveGraph(key));
}
});
Future<String> value = graphs.get("Some Key");
if(value.isDone()){
// This will block the execution until data is loaded
String success = value.get();
}

但是由于 GWT 没有Future的实现,你需要像这样创建一个

public class FutureResult<T> implements AsyncCallback<T> {
private enum State {
SUCCEEDED, FAILED, INCOMPLETE;
}

private State state = State.INCOMPLETE;
private LinkedHashSet<AsyncCallback<T>> listeners = new LinkedHashSet<AsyncCallback<T>>();
private T value;
private Throwable error;

public T get() {
switch (state) {
case INCOMPLETE:
// Do not block browser so just throw ex
throw new IllegalStateException("The server response did not yet recieved.");
case FAILED: {
throw new IllegalStateException(error);
}
case SUCCEEDED:
return value;
}
throw new IllegalStateException("Something very unclear");
}

public void addCallback(AsyncCallback<T> callback) {
if (callback == null) return;
listeners.add(callback);
}

public boolean isDone() {
return state == State.SUCCEEDED;
}

public void onFailure(Throwable caught) {
state = State.FAILED;
error = caught;
for (AsyncCallback<T> callback : listeners) {
callback.onFailure(caught);
}
}

public void onSuccess(T result) {
this.value = result;
state = State.SUCCEEDED;
for (AsyncCallback<T> callback : listeners) {
callback.onSuccess(value);
}
}

}

你的实现将变成:

    LoadingCache<String, FutureResult<String>> graphs = CacheBuilder.newBuilder().build(new CacheLoader<String, FutureResult<String>>() {
public FutureResult<String> load(String key) {
FutureResult<String> result = new FutureResult<String>();
return service.createExpensiveGraph(key, result);
}
});

FutureResult<String> value = graphs.get("Some Key");

// add a custom handler
value.addCallback(new AsyncCallback<String>() {
public void onSuccess(String result) {
// do something
}
public void onFailure(Throwable caught) {
// do something
}
});
// or see if it is already loaded / do not wait
if (value.isDone()) {
String success = value.get();
}

当使用 FutureResult 时,您不仅会缓存执行,还会获得某种惰性,因此您可以在数据加载到缓存中时显示一些 loading screen

关于java - 如何使用 Guava 在 GWT 中缓存服务器结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34851180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com