You can do this work, but I think there are better solutions.
First, if you want to use Spring MVC hooks, you'll use the postHandle method to store something in your cache and preHandle to check the cache and possible workaround. The question is what do you store in cache. You will need to save the full answer. This means that you will need to easily get the full answer from your ModelAndView in postHandle. This may or may not be easy, depending on how you do it.
Most likely, you are better off using a different caching mechanism. I recommend caching at the web server level. This is especially true if you are looking for cache at the interceptor level, as it is right βnext toβ the web server, and I see no benefit in reinventing the wheel. Apache has a cache module. So does nginx. The varnish is also very cool.
I should also mention that you should not cache until you decide what you need (prematurely optimize). This is a waste of time and effort. Secondly, when you determine that you have performance issues that need to be fixed (and caching is the right solution), you must cache the right data in the right place.
Now say that you have determined that you have performance problems, and some kind of caching is a good solution. The next thing to determine is what can be cached. If you return the same data for each URL, then caching on the web server (level Apache, nginx, Varnish, etc.) is the best choice.
Often you will have cases when two clients get to the same URL and receive different data. This is most easily seen on Facebook. I see different data when I logged in than my friend. In this case, you will not be able to cache at the web server level. You will need to cache inside your application. This usually means database level caching.
three-cups
source share