Is it possible to create a cacheable StreamResource?

Is it possible to create a dynamic Image which has the same URL so that the browser/CDN can cache it. In my case I am serving up images of products in a store and it would be great to be able to cache them. I can take care of the unique URL myself. I also don’t want to create a separate servlet for this because that involves more database connections.

The URL that StreamResource creates looks like this: VAADIN/dynamic/resource/1/2bdfa8d9-e86b-441d-a2ea-0d18f4bfdd04/image.jpg

I tried extending the StreamResource class and overriding the getId() to return my own unique image identifier instead of the UUID 2bdfa8d9-e86b-441d-a2ea-0d18f4bfdd04, but there is still the “/1/” which seems to be some kind of counter that increments for each view, thus rending caching impossible.

Any ideas?
Thanks

Hi Franz,

I just had a quick look at the StreamResource and Registry and it looks like stream resources are registered by session. If that is correct, you may have do a lot of manual work to make it work. I could be wrong though.

Another option that may or may not work for you would be to store images in a folder om the server and just serve them with Image tags.

Franz Schöning:
…I also don’t want to create a separate servlet for this because that involves more database connections.

Of course if you have a separate servlet and you have to get the image from the database every time it needs database connections. You could avoid that if you build a simple cache, like a ConcurrentHashMap<String, File> to store references from URL to a temporary file on the disk. Every time a product is viewed you could check if the url exists an if not, store the image on the filesystem and put an entry into the map.

The servlet should then use that map to get the real file for an url and stream it.

That would be dynamically, fail-safe and cacheable.

Regards