Java 如何缓存从自动完成API检索的搜索结果
我一直在使用搜索自动完成功能,数据来自JsonP请求 前面的创建方法如下所示Java 如何缓存从自动完成API检索的搜索结果,java,gwt,autocomplete,jsonp,bootstrap-typeahead,Java,Gwt,Autocomplete,Jsonp,Bootstrap Typeahead,我一直在使用搜索自动完成功能,数据来自JsonP请求 前面的创建方法如下所示 private Typeahead<Location> createTypeAhead() { typeAhead = new Typeahead<>(new Dataset<Location>() { @Override public void findMatches(final String query, final SuggestionC
private Typeahead<Location> createTypeAhead() {
typeAhead = new Typeahead<>(new Dataset<Location>() {
@Override
public void findMatches(final String query, final SuggestionCallback<Location> callback) {
requestCounter--;
startSendingRequest = true;
clear.setIcon(IconType.SPINNER);
clear.setIconSpin(true);
final Set<Suggestion<Location>> suggestions = new HashSet<>();
queryLower = query.toLowerCase();
JsonpRequestBuilder jsonpRequestBuilder;
if (!streetSelected) {
jsonpRequestBuilder = new JsonpRequestBuilder();
jsonpRequestBuilder.requestObject("https://dawa.aws.dk/vejnavne/autocomplete?side=1&per_side=500&noformat=1&q=" + queryLower + "*", new AsyncCallback<MyJsArray<VejAutocomplete>>() {
@Override
public void onFailure(Throwable caught) {
Notify.notify("suggestion matches failed");
}
@Override
public void onSuccess(MyJsArray<VejAutocomplete> result) {
Set<Location> locationSet = new LinkedHashSet<>();
for (VejAutocomplete item : result.getAsList()) {
String lowerCase = item.getTekst().toLowerCase();
if (lowerCase.startsWith(queryLower)) {
locationSet.add(new Location(Location.LocationType.STREET, item.getTekst(), item));
locationArrayList.clear();
locationArrayList.addAll(locationSet);
}
}
}
});
}
for (Location address : locationArrayList) {
String value = address.getValue();
Suggestion<Location> s = Suggestion.create(value, address, this);
if (address.getValue().toLowerCase().startsWith(queryLower)) {
suggestions.add(s);
}
}
callback.execute(suggestions);
if (typeAhead.getValue().length() != 0 && queryLower.length() <= 5 && requestCounter < 5 && requestCounter > 0) {
new Timer() {
@Override
public void run() {
findMatches(queryLower, callback);
}
}.schedule(500);
} else {
clear.setIconSpin(false);
clear.setIcon(IconType.CLOSE);
requestCounter = 5;
}
}
});
return typeAhead;
}
private-Typeahead createTypeAhead(){
typeAhead=新的typeAhead(新数据集(){
@凌驾
公共void findMatches(最终字符串查询、最终建议回调){
请求计数器--;
startSendingRequest=true;
清除.setIcon(IconType.SPINNER);
清除。setIconSpin(true);
最终集建议=新HashSet();
queryLower=query.toLowerCase();
JsonpRequestBuilder JsonpRequestBuilder;
如果(!streetSelected){
jsonpRequestBuilder=新的jsonpRequestBuilder();
jsonpRequestBuilder.requestObject(“https://dawa.aws.dk/vejnavne/autocomplete?side=1&per_side=500&noformat=1&q=“+queryLower+”*”,新的AsyncCallback(){
@凌驾
失败时的公共无效(可丢弃){
Notify.Notify(“建议匹配失败”);
}
@凌驾
成功时公共无效(MyJsArray结果){
Set locationSet=new LinkedHashSet();
对于(VejAutocomplete项:result.getAsList()){
字符串小写=item.getTekst().toLowerCase();
if(小写的startsWith(querypower)){
添加(新位置(Location.LocationType.STREET,item.getTekst(),item));
locationArrayList.clear();
locationArrayList.addAll(locationSet);
}
}
}
});
}
用于(位置地址:locationArrayList){
字符串值=address.getValue();
建议s=建议。创建(值、地址、此);
if(address.getValue().toLowerCase().StartWith(queryLower)){
建议.添加(s);
}
}
执行(建议);
if(typeAhead.getValue().length()!=0&&queryLower.length()0){
新计时器(){
@凌驾
公开募捐{
查找匹配(查询权限、回调);
}
}.附表(500);
}否则{
清除。setIconSpin(false);
clear.setIcon(IconType.CLOSE);
请求计数器=5;
}
}
});
提前返回;
}
结果如下所示:
我使用递归发送了4-5次请求,因为它没有显示带有单字母关键字的建议列表。而且它仍然不能处理一些像“s”或“e”这样的单个字母。已成功从API检索数据,但未显示在建议列表中,如下所示:
我假设我应该缓存所有搜索结果,然后从头开始重新创建自动完成,在这种情况下会变得复杂
解决这个问题有什么好办法吗