Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java save JSP page in cache

I want to use cache to load JSP page. I have created a Dynamic Web Project using Java JSP Servlet.

In this Project I am getting the data (in JSON) from Rest API call and rendered this data into JSP page inside table, dynamically.

I am also getting the Key Name 'lastUpdate' with data time in API data.

For the first time I have to rendered data inside table then second time after calling the Rest API, I have to check if my 'lastUpdate' (which is available in cache) time is same as 'lastUpdate' which is coming from Rest API call then I have to show the same page using cache.

I am not sure it possible or not, I am new in cache management.

So please share your feedback and some examples or tutorial which I can used for reference.

like image 546
user3441151 Avatar asked Jul 02 '18 10:07

user3441151


2 Answers

You could make use of the dates by a header line like:

'If-Modified-Since': 'Fri, 06 Jun 2018 01:16:45 GMT'

This tells the browser to use browser cache for the data, if it is not modified since that time.

Server uses field to communicate when data is created:

'Last-Modified': 'Fri, 06 Jun 2018 10:15:25 GMT'

When you ask If-Modified-Since you either get 304 (Not Modified) or 200 OK, with new Last-Modified value.

like image 77
mico Avatar answered Oct 18 '22 20:10

mico


This is what I understand from your question:

  1. You get a HttpRequest to your server
  2. You fetch the data from REST API
  3. The data response contains a lastUpdated time
  4. You want to render the page and make a note of the data timestamp
  5. Next time if you get the same request, you check the REST API, and updatedTime remains same, you want to return the same old rendered string/byte[]

Assumption: The page is common for all customers, and there are no search queries etc. So, the only question is whether to render the page again or not. i.e., we are not going to hold several JSPs in cache. Only "return same jsp" or "reender it again"

Here are my thoughts:

  1. Are you sure you need to cache the JSP? Think about request processing time... 90% of time will be used in making the REST Api call, and only 10% (or less) will be used to render the JSP. So, you can gain very little from caching it.
  2. The size of a rendered JSP page is likely to be big (5kb to 50kb maybe?). So, keeping it in cache can be a bigger problem. Regenerating is better. (CPU is cheap, short term memory is cheap, long term memory is costly as it can lead to GC pause)
  3. Does it have to be a JSP? There are some options like Thymeleaf and other templating engines. They can help with rendering the html string. (Because JSP is more closely tied to the ServletOutputStream and may be a bit of challenge to cache. Templating engines would probably ease the caching process as they are not closely knit)

If you are still interested to cache the JSP, this might work:

  1. If data has changed / first time: Make a wrapper of HttpServletResponse which will give a replacement stream instead of real servlet output stream. (It should keep a copy and forward to real servlet output stream also. Use the response wrapper to call forward:

    request.getRequestDispatcher(xyz.jsp").forward(req,resWrapper);

  2. If data has not changed, take the backup from step1 and write to output:

    PrintWriter out = response.getWriter(); out.write( backupStr );

like image 35
Teddy Avatar answered Oct 18 '22 19:10

Teddy