How to use / not use cache for every request?

I have a Hibernate + Ehcache + Spring MVC webapp set up in a pretty standard way:

Controller -> Service class -> DAO class -> DB

The application is a REST API and responds to HTTP GET requests using XML. Everything works as expected. DB is only hit when needed, otherwise results come from Ehcache.

I would like to use / not use the cache for every request. I would like the default behavior to be of course using the cache, but when the "nocache = 1" parameter was passed, the cache should not be used and the DB should be read instead.

I started adding the "useCache" parameter to my service class methods and then to the DAO classes, but that was not the case. Is there a better / better way to do this? Perhaps with some kind of servlet filter?

+3


source to share


1 answer


Here's one idea, but I'm not sure if it's correct.

Write 2 methods, one with query.setCacheable(true);

and another normal. Now that you can use the methods as per your needs, i.e. When you need to cache and when you don't.



Here is an article that talks about the request cache.

0


source







All Articles