Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx WebView cache

I have been trying to implement a disc based cache for WebView but with only partial success, I am particularly trying to cache the .js javascript external files which slow down the loading of javascript web pages a lot.

The Oracle documentation states that: "When working with the WebView component, you should remember that it has the default in-memory cache. It means that any cached content is lost once the application containing the WebView component is closed. However, developers can implement cache at the application level by means of the java.net.ResponseCache class. "

but this is not the case. I implemented an in-memory cache using the java.net.ResponseCache class but it is very rarely used by WebView - from time to time it stores and retrieves favicon.png from the cache - no performance gain.

I confirmed by analysing the net traffic that WebView is not caching, thus confirming what is stated in JDK-8014501: "While navigating with JavaFX WebView component javafx.scene.web.WebView, it was found, that every request retrieves all resources from the server each time even if previous activities have just retrieved the resources. This behaviour was verified by capturing and analyzing the network traffic. The performance impact is considerable. "

nothing seems to have come out of JDK-8014501, so I then wrote a cache handler using "URL.setURLStreamHandlerFactory" to intercept all URLConnections to the default sun handler. I had some success with this and was able to cache .js javascript files and increase performance significantly, but there were bugs handling some web sites, notably Outlook's email.

On looking into the way my code was handled, I found for example that the URLLoader code was setting setUsesCaches(false) with the following comments in the code (at line 279 of URLLoader.java in current 1.8.0_66 code):
// Given that WebKit has its own cache, do not use
// any URLConnection caches, even if someone installs them.
// As a side effect, this fixes the problem of WebPane not
// working well with the plug-in cache, which was one of
// the causes for RT-11880.

So can somebody out there please give me a heads up on what is really going on?

  • Oracle documentation says WebKit has an in-memory cache which can be overriden by ResponseCache, this is not the case.
  • JDK-8014501 states the problem, but has been flagged "not an issue". Why is it not an issue?
  • signifant coding around "URL.setURLStreamHandlerFactory" and "URLConnection" interception produces a functional cache with significant performance gains, but is undone by problems deliberately introduced into URLLoader code.

Thanks in advance for any feedback,

like image 901
JohnnyBeGood Avatar asked Oct 17 '15 09:10

JohnnyBeGood


1 Answers

I have been working around the non-caching WebView by implementing my own classes derived from HttpUrlConnection and HttpsUrlConnection and by using my own implementation of URLStreamHandlerFactory.

Basically I intercept all outgoing http and https requests, check if I have the data in my cache. If not I load the data from the original resource and store in in a cache directory. If I already have the data, I deliver it from my cache.

I have not implement cache header etc, because for my use case this was not necessary.

There is too much code to post here, but if you're interested, you might check out the code at mapjfx and check especially the classes in the com.sothawo.mapjfx.offline package.

This solution leaves the WebView totally unaware of the caching implementation.

like image 164
P.J.Meisch Avatar answered Oct 03 '22 12:10

P.J.Meisch