Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does caching work in JAX-RS?
    primarykey
    data
    text
    <p>Suppose I have the following web service call using <code>@GET</code> method:</p> <pre><code>@GET @Path(value = "/user/{id}") @Produces(MediaType.APPLICATION_JSON) public Response getUserCache(@PathParam("id") String id, @Context HttpHeaders headers) throws Exception { HashMap&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;(); map.put("id", id); SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession(); Cre8Mapper mapper = session.getMapper(Cre8Mapper.class); // slow it down 5 seconds Thread.sleep(5000); // get data from database User user = mapper.getUser(map); if (user == null) { return Response.ok().status(Status.NOT_FOUND).build(); } else { CacheControl cc = new CacheControl(); // save data for 60 seconds cc.setMaxAge(60); cc.setPrivate(true); return Response.ok(gson.toJson(user)).cacheControl(cc).status(Status.OK).build(); } } </code></pre> <p>To experiment, I slow down the current thread 5 seconds before fetching data from my database.<br> When I call my web service using <a href="https://addons.mozilla.org/en-us/firefox/addon/poster/" rel="noreferrer">Firefox Poster</a>, within 60 seconds it seemed much faster on the 2nd, 3rd calls and so forth, until it passed 60 seconds.<br> However, when I paste the URI to a browser (Chrome), it seemed to slow down 5s everytime. And I'm really confused about how caching is actually done with this technique. Here are my questions: </p> <ol> <li>Does POSTER actually look at the header <code>max-age</code> and decide when to fetch the data? </li> <li>In client side (web, android....), when accessing my web service do I need to check the header and then perform caching manually or the browser already cached the data itself? </li> <li>Is there a way to avoid fetching data from the database every time? I guess I would have to store my data in memory somehow, but could it potentially run out of memory? </li> <li><p>In this tutorial <a href="https://devcenter.heroku.com/articles/jax-rs-http-caching" rel="noreferrer">JAX-RS caching tutorial</a>: How does caching actually work? The first line always fetch the data from the database:</p> <p>Book myBook = getBookFromDB(id);</p></li> </ol> <p>So how it is considered cached? Unless the code doesn't execute in top/down order.</p> <pre><code> @Path("/book/{id}") @GET public Response getBook(@PathParam("id") long id, @Context Request request) { Book myBook = getBookFromDB(id); CacheControl cc = new CacheControl(); cc.setMaxAge(86400); EntityTag etag = new EntityTag(Integer.toString(myBook.hashCode())); ResponseBuilder builder = request.evaluatePreconditions(etag); // cached resource did change -&gt; serve updated content if (builder == null){ builder = Response.ok(myBook); builder.tag(etag); } builder.cacheControl(cc); return builder.build(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload