1

I am trying to cache ResponseEntity in spring boot but unable to find a proper way to implement the same.

There are few examples where

 return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(20, TimeUnit.SECONDS)) .body(body); 

But my problem here is that I am using an external library that returns

ResponseEntity<Resource> 

and I wanted to cache that response. so it should be like

ResponseEntity<Resource> getResource() { ResponseEntity<Resource> resource = getResourceFromExternalFunction(); // wanted to cache this resource return cachedResource } 

If I apply above technique for my code, it would be like

ResponseEntity<Resource> getResource() { ResponseEntity<Resource> resource = getResourceFromExternalFunction(); return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(20, TimeUnit.SECONDS)) .body(resource ); } 

it would return

ResourceEntity<ResourceEntity<Resource>> 

which is not expected. Can some one help here please

1 Answer 1

0

Always be on the look out for SpringBoot libraries that already exist before you try and reinvent the wheel. Lucky for you, the Spring community already has such a caching library which should make your life much easier, you can find a tutorial on it here:

Baeldung: A Guide to Caching in Spring

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.