在JavaFX WebView中进行缓存 [英] Caching in JavaFX WebView

查看:339
本文介绍了在JavaFX WebView中进行缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发JavaFX 2.x应用程序,该应用程序需要提供一些GIS支持.我们得出的结论是,通过嵌入式WebView使用GoogleMaps是最快的选择.它的问题在于,每当我们的应用程序启动时,就会下载相应的JavaScript库.这使得开发非常困难,因为在WebView面板上进行任何交互式操作都需要花费几秒钟的时间.

We're developing a JavaFX 2.x application, which needs to provide some GIS support. We've come to conclusion that using GoogleMaps via an embedded WebView is the quickest option. The problem with it is that every time our application starts, the corresponding JavaScript libraries are downloaded. This makes development very difficult as it takes a couple of second before anything interactive can be done on the WebView panel.

首先想到的是像Web浏览器一样进行某种缓存,以便存储库并在需要时在本地读取它们.如何使用WebView做到这一点?在我们的案例中,缓存还有其他选择吗?

The first thing that comes to mind is to have some sort of caching like web browsers do in order to store the libraries and read them locally when needed. How can this be achieved with WebView? Are there any alternatives to caching in our case?

感谢.

推荐答案

WebView组件不提供开箱即用的Web资源缓存.但是,它确实利用java.net堆栈进行网络通信.这意味着您可以安装自己的URL处理程序,该处理程序与高速缓存进行对话并提供该高速缓存中的资源.例如,在JavaFX启动调用之前,在您的main()方法中放置类似以下代码的块:

The WebView component doesn't provide caching of web resources out of the box. However, it does utilize the java.net stack for network communications. What this means is you can install your own URL Handler that talks to a cache and serves resources from that cache. For example, put something like this block in your main() method before the JavaFX launch call:

URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
    public URLStreamHandler createURLStreamHandler(String protocol) {
        if ( "http".equals(protocol) ) {
            return new URLStreamHandler() {
                protected URLConnection openConnection(URL u) throws IOException {
                    if ( isCached(u) ) {
                        return new CachedStreamConnection(u);
                    }
                    return new MyURLConnection(u);
                }
            };
        }
        // Don't handle a non-http protocol, so just return null and let
        // the system return the default one.
        return null;
    }
});

当然,细节在于魔鬼.存储时,您应该考虑HTTP标头(如 ETag )返回的缓存策略.缓存中的资源.另一个考虑因素是HTML元标记. 此处是缓存方面的好资源.

Of course the devil is in the details. You should take the cache policies returned by the HTTP headers (like ETags) into consideration when storing a resource in your cache. Another consideration is HTML meta tags. Here is a good resource on caching.

您可能还需要考虑使用cookie管理系统来补充此缓存管理系统.

You may also want to consider a cookie management system to complement this cache management system.

这篇关于在JavaFX WebView中进行缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆