Spring Data Rest - Caching
Asked Answered
A

1

6

How to enable caching with Spring Data Rest?

The reasoning behind is that repository listing and search methods won't change once the application is up. Also if the data behind the rest API is changed only through rest API it does makes a case to enable caching data too.

I believe some level is caching happens in REST API framework and it would be ideal if the caching happens at the final response stage i.e., json response (to avoid the overhead of marshalling objects to json)

Thoughts/comments?

Advocaat answered 21/8, 2013 at 15:53 Comment(0)
M
5

The easiest way to implement this on the repository level is to use Spring's @Cacheable as it can be seen in the Spring Data JPA Examples project.

Another option is to leverage HTTP Caching using the Apache HTTPD settings or a Varnish for example.

Maryellen answered 21/8, 2013 at 17:18 Comment(6)
Repository caching I agree with. However I am little bit concerned to turn on http caching where in scenario where an application refreshes it's cache through some trigger (internal or external) and we have no control to invalidate http caching.Advocaat
I see the header "ETags" in code snippets from spring data rest documentation. Not sure if it is feature of data rest api or it's a feature of rest shell. In either case any pointers to understand the implementation?Advocaat
Re: HTTP Caching - How to set up your caches is totally driven by your domain but yeah, it requires careful thinking and proper setup. No of the shelf recommendations here. Re: ETags - they're usually implemented by a Servlet filter or a HandlerInterceptor. A quick OOTB solution is the ShallowEtagHeaderFilter but that will still need the backend logic invoked as it only compares hashes of to be rendered content.Maryellen
Thank you @Oliver. Wondering if there could a feature in rest API to hook handlerInterceptor that would do similar logic to ShallowEtagHeaderFilter but before the content is rendered and still respecting jackson settings in rendering content. More than hashcode() function check considering jackson settings. This could be little more efficient, my opinion. Thoughts?Advocaat
Looks like link is broken and should now point to github.com/spring-projects/spring-data-examples.Kizer
Fixed broken linkTimber

© 2022 - 2024 — McMap. All rights reserved.