Cache-control annotations

RESTEasy provides the @Cache and @NoCache annotations, following the JAX-RS specifications, with the @GET annotation for controlling the cache. These annotations can be applied at class and method levels.

The @NoCache annotation is used to specify when caching is not required; hence, the server needs to respond back with a fresh response for every request.

The @Cache annotation is used when the caching of a response is required. Given ahead is a brief list of attributes used to control the caching behavior. Take some time to refer to the previous chapter, which talks in detail about each of these attributes.

  • maxAge: This indicates the maximum time the response message will remain in the cache.
  • sMaxAge: This is the same as maxAge, but it applies for a proxy cache. 
  • noStore: This is used to avoid caching sensitive information set to true.
  • mustRevalidate: If this is true, it revalidates the cache content and serves the fresh response.
  • proxyRevalidate: This is the same as mustRevalidate, but it applies for a proxy cache. 
  • isPrivate: If this is true, the response messages will be cached for a single user only and will not be shared. If false, it means that the response messages can be cached by any cache. 

Given ahead is the application of cache-control annotations for caching the response of the findDepartment function of DepartmentService:

//The response of findDepartment method will be cached for the specified maxAge //duration and user will get the response from cache for same department id //request until the cache expires.

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
@Cache(maxAge=2000,mustRevalidate = false,noStore = true, proxyRevalidate = false, sMaxAge = 2000)
public Departments findDepartment(@PathParam("id") Short id) {
return entityManager.find(Departments.class, id);
}

In the preceding code snippet, when the client invokes the findDepartment function, the server will respond back with the response from the cache if it exists; otherwise, it will execute the function and return the response. In the latter case, the response will be cached as per the caching definition and will be used to serve future requests. 

To set up the server-side cache, you must register an instance
of org.jboss.resteasy.plugins.cache.server.ServerCacheFeature via your application's getSingletons() or getClasses() method. The underlying cache is Infinispan. By default, RESTEasy will create an Infinispan cache for you.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.36