javafan
javafan

Reputation: 1539

Implementing Caching in REST (JAX - RS)

I am trying to learn how caching works in REST. I know all headers like Cache control, Max-age, Expires etc. I was going through example mentioned in this post.

What I know about Http cache is (I may be wrong), browser sends Http request to server, and if it has cache headers, browser will store the response in local cache. If client hits another request for the same response, browser will check the cache and if response is not expired, then it will return from cache instead of requesting to server.

Example given in this link, client hits server every time and server checks if client has expired copy or not. In this case, we hit server every time instead of retrieving data from cache.

Am I missing something here?

Upvotes: 0

Views: 1837

Answers (2)

Abhilash Kant
Abhilash Kant

Reputation: 368

This might be of some help :

Cache response only for GET request when response is 200 OK,

Test environment : Jboss6.4 and maven 3.0

Dependency :

<dependency>
  <groupId>org.jboss.resteasy</groupId>
  <artifactId>resteasy-cache-core</artifactId>
  <version>Any version after 3.0</version>
</dependency>

Code Changes : Add singleton for ServerCacheFeature in your application class.

singletons.add(new ServerCacheFeature());

Add this annotation to your function :

@Cache(maxAge=15, mustRevalidate = false, noStore = false, proxyRevalidate = false, sMaxAge = 15)

noStore can be use to enable/disable to cache resp

Upvotes: 1

Ivan Makhnyk
Ivan Makhnyk

Reputation: 106

In mentioned post server side cache is used.

In other words:

RESTEasy Cache can avoid calling UserDatabase if it already contains requested User (by EntityTag key, based on user ID).

Everything is done on server side. It has no any connection with expire date/time request/response headers.

Upvotes: 1

Related Questions