George Michael
George Michael

Reputation: 234

Server Side Caching

I have a standalone WildFly 9.0.2 and I want to cache on the server side the responses for certain requests.

Some of the requests are available for all users (visitors), others should be available only to authenticated users.

I do not understand from the documentation how to do this. Can you point me to a tutorial or manual that implements this functionality?

I started wildfly using the default configuration for Infispan that is found in the standalone\configuration\standalone.xml

Then, I modified the response object to contain in the header information for caching, hoping it would work like JAX-RS where it would check the headers and automatically cache.

final HttpServletResponse response
long current = System.currentTimeMillis();
long expires = current + 86400000;
response.setHeader("Cache-Control", "no-transform, max-age="+ 86400 + ", public");
response.addDateHeader("Expires", expires);
response.addDateHeader("Last-Modified", current);

That unfortunately did not work on the server side (thought it did work for my web application which is reading properly the header information for cache and it re-uses its local cache).

When I tried to view the Infinispan settings from the administration panel at http://127.0.0.1:9990, I get an exception and cannot proceed.

Thank you in advance for your help.

Upvotes: 0

Views: 1006

Answers (1)

cruftex
cruftex

Reputation: 5723

There is no standalone Java servlet server that does response caching the way you anticipated. The headers you set in the response, will be interpreted by the browser (which does cache) or intermediate proxies, which might cache also. Specialized proxies for caching are: Varnish, NGINX. These proxies are also called Edge Proxies.

Crafting a library that enables a standalone server to cache like you want to, seams possible, the normal request flow could be intercepted by a ServletFilter. I don't know of any public library that is doing something like that.

If you want to cache inside the application, the normal thing to do is to use a caching library, like EHCache, cache2k, Google Guava Cache and others.

In your specific example case I would recommend, that you become familiar with a proxy cache server like NGINX and put it in front of your application. That is the, let's say, the "industry standard". It is not desired to do HTTP response caching inside the Java server, for a couple of reasons:

  • In case of a cache hit, the response from the proxy is faster and the Java server is not hit
  • You can scale, by putting more caching proxies in front of your application
  • The Java heap is not a good fit to cache a large amount of data. Where should it go? There are caches that do overflow to disk. This needs complex setup, as well as a caching proxy in front of your application
  • For debugging and transparency it is better that the server generates a fresh answer when a request is sent to it

I always recommend to do caching inside the application, too. However we do it on a Java object level. The cache size is limited, so the heap keeps small. A lot of cached objects inside the application are used for many different responses, so object caching is on a more finer level then HTTP response caching.

Only in some special cases we do something similar to HTTP response caching inside the application, too. This is used to compress or recompress some images and CSS resources that are used very often. Here is some potential that is a general useful thing. Maybe we will open source this.

Hope that helps.

Upvotes: 2

Related Questions