1

I have a Jersey (version 2) resource that, during request handling, creates an object that is used throughout the request.

I would like to cache this object in a thread local storage so that other request handlers that use the same thread could re-use this object.

Is this possible? How is this done in Jersey? I am totally new to Jersey and to Java in general, please help. Can I use javax.ws.rs.container.ContainerRequestContext for this? How? How do I clear the cache?

EDIT:

Specific questions:

  1. If I use javax.ws.rs.container.ContainerRequestContext to cache my objects, at what point can I clear the cache?

  2. Is clearing the cache necessary at all in this case?

  3. Is it a good practice to use thread storage as a cache in servlets?

akonsu
  • 28,824
  • 33
  • 119
  • 194
  • 1
    Does this help? http://stackoverflow.com/questions/32854944/using-server-request-and-response-filters-for-threadlocal-storage-in-a-resteasy – Dinesh Babu K G Sep 16 '16 at 05:08
  • @kgdinesh, thanks. I have seen this question, that is why I asked if I can use `javax.ws.rs.container.ContainerRequestContext`. What I do not know is how to clear the cache and whether clearing the cache is necessary at all. And the main question is whether it is a good practice to use thread storage as a cache in servlets. – akonsu Sep 16 '16 at 05:28
  • In that case, I would recommend you to edit your question and re-tag accordingly. – Dinesh Babu K G Sep 16 '16 at 05:30

1 Answers1

0

It is advisable to avoid caching in REST as it is supposed to be stateless. But most simple solution could be to use HttpSession object to store your object and retrieve it later and then dispose it off.

For details you can refer this question:Link

Community
  • 1
  • 1
dvsakgec
  • 3,514
  • 4
  • 28
  • 35