我正在通过 RESTlet 实现 REST。这是构建这样一个宁静的 Web 服务的惊人框架;它易于学习,语法紧凑。但是,通常,我发现当某人/某个程序想要访问某些资源时,打印/输出 XML 需要时间,我使用 JaxbRepresentation。让我们看看我的代码:
@Override
@Get
public Representation toXml() throws IOException {
if (this.requireAuthentication) {
if (!this.app.authenticate(getRequest(), getResponse()))
{
return new EmptyRepresentation();
}
}
//check if the representation already tried to be requested before
//and therefore the data has been in cache
Object dataInCache = this.app.getCachedData().get(getURI);
if (dataInCache != null) {
System.out.println("Representing from Cache");
//this is warning. unless we can check that dataInCache is of type T, we can
//get rid of this warning
this.dataToBeRepresented = (T)dataInCache;
} else {
System.out.println("NOT IN CACHE");
this.dataToBeRepresented = whenDataIsNotInCache();
//automatically add data to cache
this.app.getCachedData().put(getURI, this.dataToBeRepresented, cached_duration);
}
//now represent it (if not previously execute the EmptyRepresentation)
JaxbRepresentation<T> jaxb = new JaxbRepresentation<T>(dataToBeRepresented);
jaxb.setFormattedOutput(true);
return jaxb;
}
如您所见,您可能会问我;是的,我正在通过 Kitty-Cache 实现缓存。所以,如果某些 XML 的生成成本很高,而且看起来真的在 7 年内都不会改变,那么我将使用缓存……我也将它用于可能的静态数据。缓存的最大时间限制是在内存中保留一小时。
即使我缓存输出,有时,输出也没有响应,例如挂起、部分打印,并且在打印剩余文档之前需要一些时间。XML 文档可通过浏览器和程序访问,它使用 GET。
实际上是什么问题?如果可能的话,我也很想知道 RESTlet 开发人员的答案。谢谢